使用 lxml 和请求进行 Web 抓取
Web scraping with lxml and requests
我有一个web page with hotels, where i want to get all the hotel names. I made a code following instructions from this page,但没有成功。
我的代码在这里:
from lxml import html
import requests
page = requests.get('web page url')
tree = html.fromstring(page.content)
hotel_name = tree.xpath('//span[@title="sr-hotel__name"]/text()')
print(hotel_name)
我得到的只是一个空列表。有帮助吗?
您需要添加用户代理:
from lxml import html
import requests
headers= {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"}
page = requests.get('http://www.booking.com/searchresults.et.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaEKIAQGYAQu4AQbIAQzYAQHoAQH4AQuoAgM&sid=1bc09296ee139ec3cb0edce87d7fb20a&dcid=1&class_interval=1&dest_id=67&dest_type=country&dtdisc=0&group_adults=2&group_children=0&hlrd=0&hyb_red=0&inac=0&label_click=undef&nha_red=0&no_rooms=1&postcard=0&redirected_from_city=0&redirected_from_landmark=0&redirected_from_region=0&review_score_group=empty&room1=A%2CA&sb_price_type=total&score_min=0&src=index&ss=Eesti&ss_all=0&ss_raw=Eesti&ssb=empty&sshis=0&traveller=other&nflt=ht_id%3D204%3B&lsf=ht_id%7C204%7C221&unchecked_filter=hoteltype'
, headers=headers)
tree = html.fromstring(page.content)
print(page.text)
hotel_name = tree.xpath('//span[@class="sr-hotel__name"]/text()')
print(hotel_name)
哪个会给你:
['\nHotel Telegraaf\n', '\nRadisson Blu Hotel Olümpia\n', '\nRadisson Blu Sky Hotel\n', '\nPark Inn by Radisson Central Tallinn\n', '\nPark Inn by Radisson Meriton Conference & Spa Hotel Tallinn\n', '\nMerchants House Hotel\n', '\nSwissotel Tallinn\n', '\nMy City Hotel\n', '\nNordic Hotel Forum\n', '\nHotel Palace by TallinnHotels\n', '\nHotel Ülemiste\n', '\nTallink City Hotel\n', '\nHotel London by Tartuhotels\n', '\nJohan Design & SPA Hotel\n', '\nThe von Stackelberg Hotel Tallinn\n']
但你应该阅读他们的 TOS:
我们的服务仅供个人和非商业用途。因此,您将不允许在我们网站上提供的内容、信息、软件、产品或服务的商业或竞争转售 link(深 link)的目的使用、复制、监控(例如,蜘蛛、抓取)、显示、下载或复制。
您检索到一个空列表,因为 html 是由 JavaScript 生成的。
所以要进行一些路线图:
- 使用google检查XHR;
- 确定参数&header执行javascript
- 通过 parameters
- 通过 enter link description here
- 并像这样组合它:requests.get(url, params=payload, header=header)
太可惜了,你应该考虑尊重网站所有者的规则和 robots.txt。
除了“Arbaz Siddiqui:还有更完整的包,比如 beautiful soup 或 scrapy selenium。
我有一个web page with hotels, where i want to get all the hotel names. I made a code following instructions from this page,但没有成功。 我的代码在这里:
from lxml import html
import requests
page = requests.get('web page url')
tree = html.fromstring(page.content)
hotel_name = tree.xpath('//span[@title="sr-hotel__name"]/text()')
print(hotel_name)
我得到的只是一个空列表。有帮助吗?
您需要添加用户代理:
from lxml import html
import requests
headers= {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"}
page = requests.get('http://www.booking.com/searchresults.et.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaEKIAQGYAQu4AQbIAQzYAQHoAQH4AQuoAgM&sid=1bc09296ee139ec3cb0edce87d7fb20a&dcid=1&class_interval=1&dest_id=67&dest_type=country&dtdisc=0&group_adults=2&group_children=0&hlrd=0&hyb_red=0&inac=0&label_click=undef&nha_red=0&no_rooms=1&postcard=0&redirected_from_city=0&redirected_from_landmark=0&redirected_from_region=0&review_score_group=empty&room1=A%2CA&sb_price_type=total&score_min=0&src=index&ss=Eesti&ss_all=0&ss_raw=Eesti&ssb=empty&sshis=0&traveller=other&nflt=ht_id%3D204%3B&lsf=ht_id%7C204%7C221&unchecked_filter=hoteltype'
, headers=headers)
tree = html.fromstring(page.content)
print(page.text)
hotel_name = tree.xpath('//span[@class="sr-hotel__name"]/text()')
print(hotel_name)
哪个会给你:
['\nHotel Telegraaf\n', '\nRadisson Blu Hotel Olümpia\n', '\nRadisson Blu Sky Hotel\n', '\nPark Inn by Radisson Central Tallinn\n', '\nPark Inn by Radisson Meriton Conference & Spa Hotel Tallinn\n', '\nMerchants House Hotel\n', '\nSwissotel Tallinn\n', '\nMy City Hotel\n', '\nNordic Hotel Forum\n', '\nHotel Palace by TallinnHotels\n', '\nHotel Ülemiste\n', '\nTallink City Hotel\n', '\nHotel London by Tartuhotels\n', '\nJohan Design & SPA Hotel\n', '\nThe von Stackelberg Hotel Tallinn\n']
但你应该阅读他们的 TOS:
我们的服务仅供个人和非商业用途。因此,您将不允许在我们网站上提供的内容、信息、软件、产品或服务的商业或竞争转售 link(深 link)的目的使用、复制、监控(例如,蜘蛛、抓取)、显示、下载或复制。
您检索到一个空列表,因为 html 是由 JavaScript 生成的。
所以要进行一些路线图:
- 使用google检查XHR;
- 确定参数&header执行javascript
- 通过 parameters
- 通过 enter link description here
- 并像这样组合它:requests.get(url, params=payload, header=header)
太可惜了,你应该考虑尊重网站所有者的规则和 robots.txt。
除了“Arbaz Siddiqui:还有更完整的包,比如 beautiful soup 或 scrapy selenium。