鱼C论坛

 找回密码
 立即注册
查看: 2299|回复: 8

关于换代理ip访问网址为什么还是不行的问题

[复制链接]
发表于 2020-8-17 22:45:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
爬取一个新闻网页,经过多次实验...好像有什么每日限制访问次数的机制(不懂是不是)。当爬取200次以上时,就会出现每个页面都是相同的内容(每次print出网址都是不同的)。所以我想用代理ip爬取可能就可以了,可是同样的,爬到一定次数再爬,就算换一个ip地址也不行,每个页面都是相同的内容。想请假一下大佬们是关于网页防爬机制的设定还是什么原因?有没有什么解决办法?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-18 09:33:50 | 显示全部楼层
是不是mac地址?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-18 09:48:29 | 显示全部楼层
发代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-18 09:49:54 | 显示全部楼层
本帖最后由 suchocolate 于 2020-8-18 09:51 编辑


MAC地址只在广播域传播(相关知识搜广播域冲突域),IP都3层了,网站看不到你的MAC。除非你和网站服务器直连或通过交换机同VLAN。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-18 21:50:04 | 显示全部楼层
  1. import requests  
  2. from bs4 import BeautifulSoup
  3. from lxml import etree
  4. from cnsenti import Sentiment
  5. import jieba
  6. import smtplib
  7. from email.mime.text import MIMEText
  8. import os
  9. import time

  10. from selenium import webdriver
  11. from selenium.webdriver.chrome.options import Options
  12. import time

  13. from  selenium.webdriver.common.keys import Keys
  14. from  selenium.webdriver.common.by import  By
  15. from selenium.webdriver.support.ui import  WebDriverWait
  16. from  selenium.webdriver.support import  expected_conditions as EC

  17. from selenium.webdriver.common.action_chains import ActionChains

  18. import json

  19. def get_and_save(url):
  20.     global save_txt
  21.     headers = {
  22.     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  23.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  24.     'Accept-Language':'en-US,en;q=0.5',
  25.     'Accept-Encoding':'gzip',
  26.     'DNT':'1',
  27.     'Connection':'close'
  28.     }
  29.    
  30.     r = requests.get(url,proxies=proxiess)   
  31.     r.encoding = 'utf-8'
  32.     html = etree.HTML(r.text)
  33.     result = html.xpath('//div[contains(@id,"zw_body")]/p/text()')

  34. #处理文本   
  35.     result = str(result)
  36.     result2 = result.replace('\\u3000','')
  37.     print(result2)
  38.    
  39.     return result2


  40. proxiess={}   
  41. #爬取网址所有新闻链接
  42. def get_url():
  43.     global proxiess

  44.     xunhuan=[1,2,3,4,5,6,7,8,9,10]

  45.     liebiao=[]
  46.    
  47.     for k in xunhuan:
  48.         print (k)
  49.         html = 'http://guba.eastmoney.com/default,1_'+str(k)+'.html'
  50.    
  51.         headers = {
  52.         'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  53.         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  54.         'Accept-Language':'en-US,en;q=0.5',
  55.         'Accept-Encoding':'gzip',
  56.         'DNT':'1',
  57.         'Connection':'close'
  58.         }
  59.    
  60.         page = requests.get(html,headers=headers)
  61.         
  62.         soup_obj=BeautifulSoup(page.content,'html.parser')
  63.         
  64.         
  65.         for link in soup_obj.findAll('a'):
  66.             if "href" in link.attrs:
  67.                
  68.                
  69.                 a = 'http://guba.eastmoney.com'+link.attrs['href']#href=‘/news,000762,954300722.html’
  70.                
  71.                
  72.                 if 'news' in a:

  73.                     liebiao.append(a)                     

  74.      
  75. #导入代理ip——————————————————————————————————————————————————————————————————————————————————
  76.     ipp=[]
  77.     with open('代理ip池.txt','r',encoding='UTF-8') as temp:
  78.         a = list(temp)
  79.         temp.close()            
  80.     ipp.append(a)
  81.     ipp = [i.rstrip('\n') for i in ipp[0]]
  82.     ipp = [i.strip('{') for i in ipp]
  83.     ipp = [i.strip('}') for i in ipp]   
  84. #导入代理ip——————————————————————————————————————————————————————————————————————————————————

  85. #实现每个ip使用260次就删除,换下一个ip
  86.     ip=0       #利用ip来召唤代理ip。每访问XX个网页就换一个ip地址      
  87.     for i in liebiao:   
  88.         if ip<260:#东方财富网一页88条新闻,每执行3页换一个ip总共可执行15页   
  89.             ip=ip+1  
  90.             proxies = ipp[0]
  91.             proxies=proxies[8:]
  92.             print('正在使用ip:'+proxies)
  93.             proxiess={'htttps':proxies}
  94.             
  95.             try:
  96.                 jiebakey(get_and_save(i))               
  97.                 print("正在爬取东方财富网......")      
  98.             except:
  99.                 print("正在爬取东方财富网...")
  100.         else:
  101.              ip=0
  102.              del ipp[0]

  103. if __name__ =='__main__':
  104.     get_url()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-18 21:53:02 | 显示全部楼层

代理ip池.txt的内容:
{'https': '46.166.151.181:5836'}
{'https': '114.239.171.181:4216'}
{'https': '114.99.12.100:4216'}
{'https': '175.6.66.48:3128'}
{'https': '47.107.240.107:8888'}
{'https': '113.100.209.237:3128'}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-18 23:02:06 | 显示全部楼层
937135952 发表于 2020-8-18 21:53
代理ip池.txt的内容:
{'https': '46.166.151.181:5836'}
{'https': '114.239.171.181:4216'}

全部代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-8-19 12:01:26 | 显示全部楼层
本帖最后由 937135952 于 2020-8-19 19:59 编辑
  1. [quote][size=2][url=forum.php?mod=redirect&goto=findpost&pid=4921454&ptid=178056][color=#999999]suchocolate 发表于 2020-8-18 23:02[/color][/url][/size]
  2. 全部代码[/quote]

  3. import requests  
  4. from bs4 import BeautifulSoup
  5. from lxml import etree
  6. from cnsenti import Sentiment
  7. import jieba
  8. import smtplib
  9. from email.mime.text import MIMEText
  10. import os
  11. import time
  12. from selenium import webdriver
  13. from selenium.webdriver.chrome.options import Options
  14. import time

  15. from  selenium.webdriver.common.keys import Keys
  16. from  selenium.webdriver.common.by import  By
  17. from selenium.webdriver.support.ui import  WebDriverWait #等待一个元素加载完成
  18. from  selenium.webdriver.support import  expected_conditions as EC

  19. from selenium.webdriver.common.action_chains import ActionChains#引入鼠标操作

  20. import json

  21. hot_stock_concept={}
  22. hot_stock_name={}


  23. def creat_name_dic():
  24. #无头浏览器功能
  25.     chrome_options = Options()

  26.     chrome_options.add_argument('--headless')
  27.     chrome_options.add_argument('--disable-gpu')

  28.     path = r'C:\Users\Administrator\Desktop\chromedriver.exe'

  29.     browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)
  30.     url ='http://quote.eastmoney.com/center/boardlist.html#concept_board'
  31.     browser.get(url)
  32.     time.sleep(1)


  33.     a=1
  34.     b=1
  35.     global hot_stock_concept,hot_stock_name
  36.     for i in range(14):   
  37.         stock_name = browser.find_elements_by_xpath("//td[contains(@class,'mywidth3')]/a")
  38.         
  39.         stock_name2=[]
  40.         for i in stock_name:
  41.             stock_name2.append(i.text)
  42.    
  43.         k=1
  44.    
  45.     #写入字典        
  46.         for i in stock_name2:
  47.             if k%2!=0:
  48.                 hot_stock_concept[i]=a
  49.                 a=a+1
  50.             else:
  51.                 hot_stock_name[i]=b
  52.                 b=b+1
  53.             k=k+1
  54.         
  55.     #点击下一页
  56.         time.sleep(1)
  57.         above=browser.find_element_by_xpath("//a[text()='下一页']").click()
  58.         time.sleep(1)
  59.         
  60.     file_name ='热点概念'+'.txt'
  61.     for i in list(hot_stock_concept.keys()):
  62.         with open(file_name,'w', encoding='utf-8') as temp:
  63.             temp.write(i+'\n')      
  64.    
  65.     browser.quit()
  66.       
  67.    
  68. #————————————————————————————————————————————————————————————————————————————————————————————————————————————

  69. #爬取ip并存储部分——————————————————————————————————————————————————————————————————————————————————————————————
  70. def get_ip():#动态的要用selemiun爬取

  71.     chrome_options = Options()
  72.     chrome_options.add_argument('--headless')
  73.     chrome_options.add_argument('--disable-gpu')
  74.     path = r'C:\Users\Administrator\Desktop\chromedriver.exe'
  75.     browser = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)
  76.     url ='https://proxy.seofangfa.com/'
  77.     browser.get(url)
  78.     time.sleep(1)

  79.     a = browser.find_elements_by_xpath("//table[contains(@class,'table')]/tbody/tr/td")               
  80.     #print(a)
  81.    
  82.     al=[]
  83.     bl=[]
  84.     for n, v in enumerate(a):#enumerate() 函数用于为可迭代对象添加序号,默认序号从0开始,一般用在 for 循环当中。
  85.         #在这里就相当于给n传入序号,v传入元素值
  86.         if n % 5 == 0 :      
  87.             print(v.text)
  88.             a=v.text
  89.             al.append(a)
  90.         elif n % 5 == 1:
  91.             print(v.text)
  92.             b=v.text
  93.             bl.append(b)
  94.     browser.quit()
  95.     #print(al)
  96.     #print(bl)
  97.    
  98.     #save_dic(hot_stock_concept)
  99.     #return hot_stock_concept

  100. #创建ip池
  101.     url = 'https://www.baidu.com/'
  102.     headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.226.188.131:4216 Safari/537.36',
  103.             'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  104.             'Accept-Language':'en-US,en;q=0.5',
  105.             'Accept-Encoding':'gzip',
  106.             'DNT':'1',
  107.             'Connection':'close'
  108.             }
  109.    
  110.     for x,y in zip(al,bl):
  111.         z=x+':'+y
  112.         proxies={'https':'%s'%z}
  113.         print(proxies)
  114.    
  115.         try:   
  116.             page = requests.get(url,proxies=proxies)
  117.             with open('代理ip池.txt','a',encoding='UTF-8') as temp:
  118.                 temp.write(str(proxies)+'\n')      
  119.         except:
  120.             print("此ip无效")
  121.             
  122.             
  123.             


  124. #存储关键字
  125. savekeyword={}
  126. s=''
  127. save_txt=''
  128. jishu_total=0
  129. total=0
  130. total_count = {}
  131. total_count_l=[]

  132. def jiebakey(key1):
  133.     global s
  134.     global savekeyword
  135.     global total
  136.     global jishu_total
  137.     global total_count
  138.     global total_count_l
  139.     #载入自建字典
  140.     jieba.load_userdict("热点概念.txt")
  141.    

  142.     txt = key1
  143.     words = jieba.lcut(txt)
  144.    
  145.     count = {}
  146.     for word in words:           
  147.         if len(word) < 2:         
  148.             continue
  149.         else:
  150.             count[word] = count.get(word, 0) + 1   
  151.             
  152.     exclude=[]
  153.     with open('热点概念备份.txt','r',encoding='UTF-8') as temp:
  154.         a = list(temp)
  155.         temp.close()            
  156.     exclude.append(a)
  157.     exclude = [i.rstrip('\n') for i in exclude[0]]

  158.     for key in list(count.keys()):     
  159.         if key in exclude:
  160.             continue
  161.         else:
  162.             del count[key]   
  163.                
  164.   
  165.     for word in count:
  166.         if len(word) > 1:
  167.             total_count[word] = total_count.get(word, 0) + 1

  168.     total_count_l = list(total_count.items())   
  169.     total_count_l.sort(key=lambda x: x[1], reverse=True)     



  170. def get_and_save(url):
  171.     global save_txt
  172.     headers = {
  173.     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  174.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  175.     'Accept-Language':'en-US,en;q=0.5',
  176.     'Accept-Encoding':'gzip',
  177.     'DNT':'1',
  178.     'Connection':'close'
  179.     }
  180.    

  181.     r = requests.get(url,proxies=proxiess)  
  182.     r.encoding = 'utf-8'
  183.     html = etree.HTML(r.text)
  184.     result = html.xpath('//div[contains(@id,"zw_body")]/p/text()')

  185.    
  186.     result = str(result)
  187.     result2 = result.replace('\\u3000','')
  188.     print(result2)
  189.    
  190.     return result2


  191. proxiess={}   

  192. def get_url():
  193.     global proxiess

  194.     xunhuan=[1,2,3,4,5,6,7,8,9,10]

  195.     liebiao=[]
  196.    
  197.     for k in xunhuan:
  198.         print (k)
  199.         html = 'http://guba.eastmoney.com/default,1_'+str(k)+'.html'
  200.    
  201.         headers = {
  202.         'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  203.         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  204.         'Accept-Language':'en-US,en;q=0.5',
  205.         'Accept-Encoding':'gzip',
  206.         'DNT':'1',
  207.         'Connection':'close'
  208.         }
  209.    
  210.         page = requests.get(html,headers=headers)
  211.         
  212.         soup_obj=BeautifulSoup(page.content,'html.parser')
  213.         

  214.         
  215.         for link in soup_obj.findAll('a'):
  216.             if "href" in link.attrs:

  217.                
  218.                 a = 'http://guba.eastmoney.com'+link.attrs['href']
  219.                
  220.                
  221.                 if 'news' in a:

  222.                     liebiao.append(a)                     

  223.      
  224. #导入代理ip——————————————————————————————————————————————————————————————————————————————————
  225.     ipp=[]
  226.     with open('代理ip池.txt','r',encoding='UTF-8') as temp:
  227.         a = list(temp)
  228.         temp.close()            
  229.     ipp.append(a)
  230.     ipp = [i.rstrip('\n') for i in ipp[0]]
  231.     ipp = [i.strip('{') for i in ipp]
  232.     ipp = [i.strip('}') for i in ipp]   
  233. #导入代理ip——————————————————————————————————————————————————————————————————————————————————

  234. #实现每个ip使用XX次就换下一个ip  _______________________________  
  235.     ip=0         
  236.     for i in liebiao:   
  237.         if ip<260:
  238.             ip=ip+1  
  239.             proxies = ipp[0]

  240.             proxies=proxies[8:]
  241.             print('正在使用ip:'+proxies)
  242.             proxiess={'htttps':proxies}
  243.                
  244.             try:
  245.                 jiebakey(get_and_save(i))               
  246.                 print("正在爬取东方财富网......")      
  247.             except:
  248.                 print("正在爬取东方财富网...")
  249.         else:
  250.              ip=0
  251.              del ipp[0]
  252. #实现每个ip使用XX次就换下一个ip  _______________________________           

  253.         
  254. if __name__ =='__main__':

  255.     creat_name_dic()
  256.     time.sleep(3)

  257.     get_url()
  258.     time.sleep(1)

  259.     time.sleep(1)
  260.     print(total_count_l)
  261.     #send_email(s)
  262.    

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-20 10:41:17 | 显示全部楼层

全部代码已发
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 01:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表