【问题标题】:Sending data fields through request.post通过 request.post 发送数据字段
【发布时间】:2020-08-18 11:34:24
【问题描述】:
from bs4 import BeautifulSoup as bs  #importing the required libraries
from urllib.request import  urlopen
import requests
urls1="https://www.makemytrip.com/hotels/" #initial url which contains the form where we could give our preferences.

#passing the data parameters
data={'checkin': '08152020',
'city': 'CTGOI',
'checkout': '08162020',
'roomStayQualifier': '2e0e',
'locusId': 'CTGOI',
'country': 'IN',
'locusType': 'city',
'searchText': 'Goa, India',
'visitorId': '5c68c2fb-0551-4ef2-8dae-1a55bb744e66'
}
req=requests.post(urls1,data, headers={'User-Agent': 'XYZ/3.0'})
page_soup = bs(req.content,"html.parser")
print(page_soup)

实际上我想抓取上述数据字段下的酒店,这就是为什么我将带有requests.post方法的数据参数发送到初始url,这样当我收到响应对象时,我会得到下一页的内容将包含符合上述要求的酒店。

【问题讨论】:

  • 请格式化您的代码。这里没有问题
  • 您好,先生,实际上我想通过 requets.post 方法发送其他数据,例如签入、结帐、地点等,这样当完成后,我将获得的响应对象将包含所有数据参数中提到的酒店列表,以便我可以抓取它。
  • 您能否通过写下您面临的相同问题来更新问题?如果您可以显示打印输出,它也会有所帮助。您可以使用这样的东西来提取: page_soup.find("input", {"id": "some_id"})["value"]
  • Bendik Knapstad ,页面汤只包含初始网址的内容,我不想要。我想要点击搜索时下一页的内容(填写地点等详细信息后,签到、结账​​时间等)
  • 谢谢大家的建议真的非常感谢!

标签: python dictionary web-scraping beautifulsoup python-requests


【解决方案1】:

您正在抓取的网站使用 GET 方法执行搜索。

它还使用不同的 URL 进行酒店搜索,https://www.makemytrip.com/hotels/hotel-listing/

稍微修改您的示例以应用 GET 请求而不是 POST 请求,我们能够获得酒店列表结果。

from bs4 import BeautifulSoup as bs

headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
# setting a "browser" header seems to be required for this site.

data = {'checkin': '08192020',
        'city': 'CTGOI',
        'checkout': '08202020',
        'roomStayQualifier': '2e0e',
        'locusId': 'CTGOI',
        'country': 'IN',
        'locusType': 'city',
        'searchText': 'Goa, India',
        'visitorId': 'aaab4f61-2069-4033-bb97-0791f0f70'}

url = 'https://www.makemytrip.com/hotels/hotel-listing/'

# adding the params argument and supplying the dictionary of search data formats the resulting URL into something that makemytrip.com can understand. 
# adding a timeout just in case makemytrip.com doesn't respond
req = requests.get(url, params=data, headers=headers, timeout=5)


page_soup = bs(req.content,'html.parser')

# this finds all the divs in the result with a class name of "listingRow".  
listing_results = page_soup.findAll('div', class_='listingRow')

# this results array can then be looped through to find more details about each listing.
for listing in listing_results:
    print(listing.find("p", itemprop="name").getText())

【讨论】:

  • 感谢@Matt Dale,感谢您对代码的详细解释,从而解决了我的查询,感谢您的努力。谢谢楼主!
  • 您是如何知道网站使用的不同 url 的,但当我点击该 url 时,我也没有被重定向。代码运行顺利
  • 对不起,我没有解释。我去了该网站并在我的浏览器中进行了搜索。按下搜索按钮后,浏览器会重定向到带有查询参数的 /hotel-listing/ URL。您还可以通过使用 Chrome 开发工具的“网络”选项卡并在执行搜索时观察请求发生的情况来查看这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2016-04-06
  • 1970-01-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多