尝试学习Xpath爬虫,通过Xpath helper获得数据99条,其中最后一条为“$PORT”,如图
代码如下,使用这个Xpath路径只能返回"$PORT",其他98条数据没了.......
import requests
import csv
from lxml import etree
url = 'https://www.msccruisesusa.com/webapp/wcs/stores/servlet/MSC_SearchCruiseManagerRedirectCmd?storeId=12264&langId=-1004&catalogId=10001&monthsResult=&areaFilter=MED%40NOR%40&embarkFilter=&lengthFilter=&departureFrom=01.11.2020&departureTo=04.11.2020&ships=&category=&onlyAvailableCruises=true&packageTrf=false&packageTpt=false&packageCrol=false&packageCrfl=false&noAdults=2&noChildren=0&noJChildren=0&noInfant=0&dealsInput=false&tripSpecificationPanel=true&shipPreferencesPanel=false&dealsPanel=false'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}
source = requests.get(url,headers=headers).content.decode('UTF-8')
html = etree.HTML(source)
portList = html.xpath('//*[@class="cr-city-name"]')
for port in portList:
print(port.xpath('string()'))
求各位大神搭救,不知道是哪里出了问题......网上遍寻不到相似的情况