【发布时间】:2013-01-04 17:15:40
【问题描述】:
我正在尝试从网站获取一些数据。但是它返回给我incomplete read。我要获取的数据是大量嵌套链接。我在网上做了一些研究,发现这可能是由于服务器错误(之前完成的分块传输编码
达到预期大小)。我还在 link
但是,我不确定如何将其用于我的情况。以下是我正在处理的代码
br = mechanize.Browser()
br.addheaders = [('User-agent', 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1;Trident/5.0)')]
urls = "http://shop.o2.co.uk/mobile_phones/Pay_Monthly/smartphone/all_brands"
page = urllib2.urlopen(urls).read()
soup = BeautifulSoup(page)
links = soup.findAll('img',url=True)
for tag in links:
name = tag['alt']
tag['url'] = urlparse.urljoin(urls, tag['url'])
r = br.open(tag['url'])
page_child = br.response().read()
soup_child = BeautifulSoup(page_child)
contracts = [tag_c['value']for tag_c in soup_child.findAll('input', {"name": "tariff-duration"})]
data_usage = [tag_c['value']for tag_c in soup_child.findAll('input', {"name": "allowance"})]
print contracts
print data_usage
请帮帮我。谢谢
【问题讨论】:
-
通常,在我收到错误后我尝试另一个请求,它总是成功。 100 次试验中可能有 100 次。
标签: python python-2.7 web-scraping beautifulsoup mechanize