【问题标题】:Avoiding 503 errors with urllib2使用 urllib2 避免 503 错误
【发布时间】:2011-10-01 21:12:41
【问题描述】:

我是使用 python 进行网络抓取的新手,所以我不知道我这样做是否正确。

我正在使用一个调用 BeautifulSoup 的脚本来解析来自 google 搜索的前 10 页的 URL。使用 stackoverflow.com 进行测试,开箱即用。我在另一个站点上测试了几次,试图查看脚本是否真的适用于更高的谷歌页面请求,然后它在我身上出现了 503。我切换到另一个 URL 进行测试并处理几个低页面请求,然后也是 503'd。现在我传递给它的每个 URL 都是 503'ing。有什么建议吗?

import sys # Used to add the BeautifulSoup folder the import path
import urllib2 # Used to read the html document

if __name__ == "__main__":
### Import Beautiful Soup
### Here, I have the BeautifulSoup folder in the level of this Python script
### So I need to tell Python where to look.
sys.path.append("./BeautifulSoup")
from BeautifulSoup import BeautifulSoup

### Create opener with Google-friendly user agent
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

### Open page & generate soup
### the "start" variable will be used to iterate through 10 pages.
for start in range(0,10):
    url = "http://www.google.com/search?q=site:stackoverflow.com&start=" + str(start*10)
    page = opener.open(url)
    soup = BeautifulSoup(page)

    ### Parse and find
    ### Looks like google contains URLs in <cite> tags.
    ### So for each cite tag on each page (10), print its contents (url)
    for cite in soup.findAll('cite'):
        print cite.text

【问题讨论】:

    标签: python screen-scraping extract beautifulsoup


    【解决方案1】:

    Google 服务条款不允许自动查询。 有关信息,请参阅本文: Unusual traffic from your computer 还有Google Terms of service

    【讨论】:

    • 这是有道理的。有没有办法使用 Google Web Search API 做到这一点?我觉得应该有,但我对 AJAX 或 Javascript 没有太多经验。
    【解决方案2】:

    正如 Ettore 所说,抓取搜索结果违反了我们的服务条款。但是请查看 WebSearch api,特别是 documentation 的底部部分,它应该为您提供有关如何从非 javascipt 环境访问 API 的提示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      • 2015-02-05
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多