【问题标题】:I am trying to web scrape http://angel.co/bloomfire我正在尝试网页抓取 http://angel.co/bloomfire
【发布时间】:2017-09-06 14:50:09
【问题描述】:

我正在尝试从 https://angel.co/bloomfire 网站上抓取数据

import requests
from bs4 import BeautifulSoup

res = requests.get('https://angel.co/pen-io')
soup = BeautifulSoup(res.content, 'html.parser')
print(soup.prettify())

这将打印标题标签为“找不到页面 - 404 - AngelList”。 在 webbrowser 中,网站运行良好,但其源代码与我的 python 脚本的输出不同。 我也将 selenium 与 phantomjs 一起使用,但它显示了同样的东西

【问题讨论】:

    标签: python python-3.x web-scraping beautifulsoup python-requests


    【解决方案1】:

    看起来 angel.co 会根据发送的 User-Agent 回复 HTTP 404,并且看起来会阻止默认的 requests 代理(可能取决于版本)。这可能会阻止机器人活动。

    我的ipython 会话的一些输出如下。我正在使用requests/2.17.3

    使用默认 Python 请求用户代理

    In [37]: rsp = requests.get('https://angel.co/bloom')
    In [38]: rsp.status_code
    Out[38]: 404
    

    使用 Mozilla 兼容的用户代理

    In [39]: rsp = requests.get('https://angel.co/bloom', headers={'User-Agent': 'Mozilla/5.0'})
    
    In [40]: rsp.status_code
    Out[40]: 200
    

    rsp.content 包含您希望从 angel.co/bloom 看到的内容。

    使用一些随机的用户代理

    In [41]: rsp = requests.get('https://angel.co/bloom', headers={'User-Agent': 'birryree angel scraper'})
    
    In [42]: rsp.status_code
    Out[42]: 200
    

    因此,您应该设置 User-Agent 以通过用于各种默认代理的任何类型的过滤/阻止天使。

    如果您要进行大量抓取,我建议您做一个好公民并设置一个代理字符串,以便他们在您的抓取引起问题时与您联系,例如:

    requests.get('https://angel.co/bloom', 
                 headers={'User-Agent': 'Mozilla/5.0 (compatible; http://yoursite.com)'}
    

    【讨论】:

      【解决方案2】:

      在请求参数中添加标头可以访问页面。这是“人们也看过”的结果。试试下面的脚本:

      import requests
      from bs4 import BeautifulSoup
      
      res = requests.get('https://angel.co/pen-io', headers={"User-Agent":"Mozilla/5.0"})
      soup = BeautifulSoup(res.text, 'html.parser')
      for item in soup.select(".text"):
          try:
              title = item.select_one("a.startup-link").get_text()
          except:
              title = ''
          print(title)
      

      结果:

      Corilla
      Pronoun
      checkthis
      Wattpad
      Medium
      Plympton
      Cheezburger
      AngelList
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        相关资源
        最近更新 更多