2017-08-03 230 views
0

我试图从lowes.com刮取产品信息。我的测试是专门为此产品AirStone 8-sq ft Autumn Mountain Faux Stone Veneer。当我在没有启用JavaScript的情况下访问该页面(为了确保我没有看到urllib /请求可能无法获取的内容),我明确地为此项目获得价格,但是当我尝试使用上面的任一包时,我缺少几个部分网页。网页抓取不检索整个文档urllib或请求

它恰好发生这些部分是我需要的刮(部分价格信息,神奇的一切仍然可用)。我宁愿不为了速度而使用硒。我目前使用的请求和urllib的期待正是如此

通用项目

from urlopen import Request, urlopen 
import requests # switch as needed with urlopen 
import gzip # manual deflation required with Request object urlopen or so I've found 

url = "https://www.lowes.com/pd/AirStone-8-sq-ft-Autumn-Mountain-Faux-Stone-Veneer/50247201" 
headers = { 
     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", 
     "Accept-Encoding": "gzip, deflate, br", 
     "Accept-Language": "en-US,en;q=0.8", 
     "Cache-Control": "no-cache", 
     "Connection": "keep-alive", 
     "DNT": "1", 
     # "Host": "www.lowes.com", Tried, no difference 
     "Pragma": "no-cache", 
     # "Referer": "https://www.lowes.com/", Tried, no difference 
     "Upgrade-Insecure-Requests": "1", 
     "User-Agent": "Mozilla/5.0 (Windows NT 6.1 Win64 x64) AppleWebKit/537.36 (KHTML," 
     " like Gecko) Chrome/59.0.3071.115 Safari/537.36" # <=- Tried placing all on one line, didn't make a difference 
    } 

的urlopen

req = Request(url, None, headers) 
page = gzip.decompress(urlopen(req).read()).decode('utf-8') 
with open("content.txt", "w") as f: 
    f.write(page) # <=- missing the 59.97 price tag anywhere in the document :(

要求

sessions = requests.Session() 
page = sessions.get(self.url, headers=headers) 

with open("content.txt", "w") as f: 
    f.write(page) # <=- Also missing the 59.97 price tag anywhere in the document :'(

所以问题是,我失去了什么?有没有理由失踪?这不是JavaScript相关的,因为我在尝试刮取数据之前故意禁用它,因为我发现这是很多时候的问题。

任何帮助将不胜感激。

+0

您要返回的页面显示“输入您的位置以获取定价和可用性” - 在实际的浏览器中,您可能从上次访问中获得了一个Cookie,用于向您的网站提供您的位置信息。我确信可以使用任一请求方法包含cookie,但我不知道详细信息。 – jasonharper

+0

这是一个惊人的发现,我以为我已经删除了cookies。显然我没有。这是非常赞赏良好的互联网居民:) Tally-Ho,看看我能找出什么 – Akidi

回答

0

根据jasonharper的评论。 Cookies最终成为答案。找到合适的人允许我提取所有必要的数据。

简而言之,在尝试抓取网站之前,如果没有其他原因,请务必禁用/删除Cookie,而不是确保您看到脚本所看到的内容。

对于那些好奇的具体cookie是{“sn”:“####”}(商店号码)你可以简单地选择一家商店,并悬停在它启用JavaScript的查看它链接到的网址找出店面号码。更改为适合