面对使用GET请求的问题

时间:2017-05-26 09:41:10

标签: python-3.x web-scraping get python-requests

当我运行我的第一个脚本时,它会获取结果,但是当我运行第二个脚本时,我什么也得不到。我在第二个脚本中找不到任何遗漏以获得所需的结果。第一个脚本中使用的URL与我在开发人员工具中找到的要求连接在一起。当我使用基本URL和参数?

时,为什么我的第二个脚本不起作用?
import requests
from lxml import html
response = requests.get("http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR0.TRC0.H0.Xfunny+bear.TRS0&_nkw=funny+bear&_sacat=237")
tree=html.fromstring(response.text)
titles=tree.xpath("//h3[@class='lvtitle']")
for title in titles:
    name=title.xpath(".//a[@class='vip']/text()")[0]
    print(name)

import requests
from lxml import html
payload={'_from':'R40','_trksid':'p2050601.m570.l1313.TR0.TRC0.H0.Xfunny+bear.TRS0','_nkw':'funny+bear','_sacat':'237'}
headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
response = requests.get("http://www.ebay.com/",data=payload,headers=headers)
tree=html.fromstring(response.text)
titles=tree.xpath("//h3[@class='lvtitle']")
for title in titles:
    name=title.xpath(".//a[@class='vip']/text()")[0]
    print(name)

1 个答案:

答案 0 :(得分:1)

首先,您的网址为http://www.ebay.com/sch/i.html。 (不仅仅是ebay.com)

然后,如果您想构建类似wwww.example.com?key1=value1&key2=value2的查询字符串,则需要使用params作为参数,而不是data

此代码应该有效。

import requests
from lxml import html
payload = {'_from':'R40','_trksid':'p2050601.m570.l1313.TR0.TRC0.H0.Xfunny+bear.TRS0','_nkw':'funny+bear','_sacat':'237'}
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'}
response = requests.get("http://www.ebay.com/sch/i.html", params=payload, headers=headers)
tree = html.fromstring(response.text)
titles = tree.xpath("//h3[@class='lvtitle']")
for title in titles:
    name = title.xpath(".//a[@class='vip']/text()")[0]
    print(name)

参考:http://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls