推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
qqhaodong
V2EX  ›  Python

爬虫为何使用 requests 代理,依然被封堵?

  •  
  •   qqhaodong · Feb 15, 2019 · 3951 views
    This topic created in 2653 days ago, the information mentioned may be changed or developed.

    我通过爬取西刺代理获取代理 list ,如下面代码

    def setProxiesConfig():
      url = 'http://www.xicidaili.com/nn/'
      headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
      }
      web_data = requests.get(url, headers=headers)
      soup = BeautifulSoup(web_data.text, 'lxml')
      ips = soup.find_all('tr')
      ip_list = []
      for i in range(1, len(ips)):
        ip_info = ips[i]
        tds = ip_info.find_all('td')
        ip_list.append(tds[1].text + ':' + tds[2].text)
      print(ip_list)
      return ip_list
    #获取随机 IP
    def get_random_ip(ip_list):
      proxy_list = []
      for ip in ip_list:
        proxy_list.append('http://' + ip)
      proxy_ip = random.choice(proxy_list)
      proxies = {'http': proxy_ip}
      return proxies
    

    获取列表后,随机取一个 IP{'http':'http://ip:9999'} 然后放到 get 中

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
      }
      ipC=get_random_ip(ip_list)
      web_data = requests.get(URL, headers=headers,proxie= ,verify=False,timeout=10)
      soup = BeautifulSoup(web_data.text, 'lxml')
    

    爬取了 46 个页面的时候还可以操作,但是之后就开始提示 timeout 了,就无论如何也不能爬取了,这是为啥?

    11 replies    2019-02-18 18:44:03 +08:00
    zarte
        1
    zarte  
       Feb 15, 2019
    开一个页面休息 5 秒试试
    exceloo
        2
    exceloo  
       Feb 15, 2019
    随机 sleep,sleep 时间大于 10 秒
    omph
        3
    omph  
       Feb 15, 2019
    time.sleep(randomint(3, 5))
    locoz
        4
    locoz  
       Feb 15, 2019 via Android
    你确定你这个代码能跑通?最下面 proxie=的位置后面都没东西了
    locoz
        5
    locoz  
       Feb 15, 2019 via Android
    而且那个参数名叫 proxies,不是 proxie。如果你的代码没问题的话,代理本身超时也是有可能的,注意看报错信息。
    SpiderXiantang
        6
    SpiderXiantang  
       Feb 15, 2019 via Android
    西刺就别提了吧
    gabon
        7
    gabon  
       Feb 15, 2019 via Android
    这种免费代理就是垃圾,基本上不可以用的,最好是买代理。。
    dongya
        8
    dongya  
       Feb 15, 2019
    用收费代理吧
    zxfgds
        9
    zxfgds  
       Feb 15, 2019
    建议上 芝麻代理之类的, 靠谱点
    zr8657
        10
    zr8657  
       Feb 16, 2019
    用之前线上 ip138 之类的检测下是否更换成功啊,免费的 ip 真的都挺垃圾
    911speedstar
        11
    911speedstar  
       Feb 18, 2019
    代理池要不断测试更新
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2779 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 47ms · UTC 01:12 · PVG 09:12 · LAX 18:12 · JFK 21:12
    ♥ Do have faith in what you're doing.