尽管网页没有改变,Python程序抓取不同的文本
Posted
技术标签:
【中文标题】尽管网页没有改变,Python程序抓取不同的文本【英文标题】:Python Program Scraping Different Text Despite Webpage Not Changing 【发布时间】:2015-10-24 08:23:01 【问题描述】:此代码尝试通过第一方亚马逊供应商抓取亚马逊列表以检查其可用性。
from lxml import html
from time import sleep
import requests
import time
Amazonurl = raw_input("Item URL: ")
page = requests.get(Amazonurl)
tree = html.fromstring(page.text)
Stock = tree.xpath('//*[@id="merchant-info"]/text()')
IfInstock = ''.join(Stock)
if 'Ships from and sold by Amazon.com.' in IfInstock:
print 'Instock'
print time.strftime("%a, %d %b %Y %H:%M:%S")
else:
print 'Not in Stock'
print time.strftime("%a, %d %b %Y %H:%M:%S")
奇怪的是,当我插入最近几天没有缺货的http://www.amazon.com/New-Nintendo-3DS-XL-Black/dp/B00S1LRX3W/ref=sr_1_1?ie=UTF8&qid=1438413018&sr=8-1&keywords=new+3ds 时,有时代码会返回“Instock”,而其他时候会返回“Not in stock” .我发现这是因为代码经常刮擦
[]
而其他时候,它会按应有的方式抓取以下内容。
['\n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n Ships from and sold by Amazon.com.\n \n \n \n \n \n \n \n \n Gift-wrap available.\n \n\n']
网页似乎没有变化。有谁知道为什么我的输出经常变化,也许是关于我如何解决这个问题的解释?提前致谢。
【问题讨论】:
只是为了确保这里的人们可以为您提供最大的帮助,我建议您分享可以执行的代码。这是为了确保我们可以复制您的问题。在这种情况下,您的脚本上的缩进是错误的。 @gglasses 删除了“while True:”,应该可以解决缩进问题。 你多久报废一次? 非常频繁。大约每 15 秒一次。亚马逊会限制来自我的程序的请求并将其标记为垃圾邮件吗? 我确认了这个问题。这可能是故意的,也可能是由于某些故障,例如最近从 ORM 缓存中刷新项目导致数据库确认超过延迟阈值。 【参考方案1】:亚马逊拒绝为您提供此页面。
我刚刚在您的脚本中添加了一行代码,只是为了查看当您获得odd
结果时响应的status_code
是什么。
from lxml import html
from time import sleep
import requests
import time
Amazonurl = "http://www.amazon.com/dp/B00S1LRX3W/?tag=stackoverfl08-20"
intent = 0
while True:
page = requests.get(Amazonurl)
tree = html.fromstring(page.text)
print(page.status_code)
Stock = tree.xpath('//*[@id="merchant-info"]/text()')
IfInstock = ''.join(Stock)
if 'Ships from and sold by Amazon.com.' in IfInstock:
print('Instock')
print(time.strftime("%a, %d %b %Y %H:%M:%S"))
else:
print('Not in Stock')
print(time.strftime("%a, %d %b %Y %H:%M:%S"))
time.sleep(15)
if intent>15:
break
intent += 1
正如您所说,我以 15 秒 的时间间隔运行此脚本。结果如下:
200
Instock
Sat, 01 Aug 2015 19:51:27
200
Instock
Sat, 01 Aug 2015 19:51:43
503
Not in Stock
Sat, 01 Aug 2015 19:51:59
200
Instock
Sat, 01 Aug 2015 19:52:15
200
Instock
Sat, 01 Aug 2015 19:52:32
200
Instock
Sat, 01 Aug 2015 19:52:48
200
Instock
Sat, 01 Aug 2015 19:53:05
200
Instock
Sat, 01 Aug 2015 19:53:22
200
Instock
Sat, 01 Aug 2015 19:53:38
200
Instock
Sat, 01 Aug 2015 19:53:55
200
Instock
Sat, 01 Aug 2015 19:54:12
200
Instock
Sat, 01 Aug 2015 19:54:29
200
Instock
Sat, 01 Aug 2015 19:54:45
200
Instock
Sat, 01 Aug 2015 19:55:02
200
Instock
Sat, 01 Aug 2015 19:55:18
200
Instock
Sat, 01 Aug 2015 19:55:35
200
Instock
Sat, 01 Aug 2015 19:55:52
您可以看到,当结果为 odd
或“无库存”时,status_code
为 503
。根据http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html对this的定义如下:
10.5.4 503 服务不可用 服务器当前无法处理请求,因为 服务器临时超载或维护。含义是 这是一种暂时的情况,经过一段时间后会得到缓解 延迟。如果已知,延迟的长度可以在一个 重试后标头。如果没有给出 Retry-After,客户端应该 像处理 500 响应一样处理响应。
Note: The existence of the 503 status code does not imply that a server must use it when becoming overloaded. Some servers may wish to simply refuse the connection.
话虽如此,亚马逊没有为您提供此页面,因为您在短时间内提出了多个请求。这种“短”时间实际上对亚马逊来说并没有那么苛刻,这就是为什么你大部分时间都得到200 status_code
。
我希望能回答你的问题。现在,如果你真的想废弃像亚马逊这样的网站,我建议你使用Scrapy
,它非常易于使用且易于配置。您可以使用随机的user-agent
来摆脱亚马逊等网站。但是,当然,这只是您原始问题的附加内容。
【讨论】:
以上是关于尽管网页没有改变,Python程序抓取不同的文本的主要内容,如果未能解决你的问题,请参考以下文章
在 python 中使用 beautifullsoup4 抓取网页时出现奇怪的文本缩进