【发布时间】: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