在相同 url 下抓取不同的值(cookies?)

Scraping different values (cookies?) under the same url

我正在使用 scrapy 和 beautifulsoup 来抓取美国不同城市的所有酒店列表。

当我进入名为 "Hotels in San Francisco" 的页面时,它只包含该市 250 家酒店中的 30 家。单击 "next 30 on the list" 不会更改 url,也不会更改排序参数。 我的问题:我怎样才能到达 250 家酒店的整个列表,或者选择要从中抓取的排名。谢谢

到目前为止我的代码:

r = requests.get(url)
soup = BeautifulSoup(r.content,'html.parser')
headers = soup.find_all("h1",{"class":"X"})

for header in headers:
    headerText = header.text
    match=re.search('(.+ Hotels)',headerText)
    if match:
        writeHotels(soup,match.group(0))



def writeHotels(soup,location):

   #create Hotels directory
   hotelDir = 'Hotels/'
   if not os.path.exists(hotelDir):
       os.makedirs(hotelDir)


   hotels = soup.find_all("a",{"class":"Y"})
   name=location+'.txt'
   #write hotels to file
   if os.path.exists(hotelDir+name):
       print 'opening file '+name+"\n"
   else:
       print 'creating file '+name+"\n"
   file=open(hotelDir+name,'a') 
   for hotel in hotels:
       file.write(hotel.text+"\n")
   file.close()

如果您在页面底部的页码中查看页面源代码,它们对每个页面都有唯一的 url。如果你打印出汤,你会看到你可以抓住那个 url。如果有很多页面,它不会显示所有页面,只显示中间页面的 ...。但是,您可以根据第一个和最后一个值计算 urls(我在下面没有这样做)。这是我使用的代码:

url = "http://www.tripadvisor.com/Hotels-g60713-San_Francisco_California-Hotels.html" 
page=urllib.request.urlopen(url)

soup = BeautifulSoup(page.read())
#print(soup)
for myValue3 in soup.findAll("a",attrs={ "class" : "pageNum" }):
    try:
        print("the value of page " + myValue3.get("data-page-number") + " is: " + myValue3.get("href").split("#ACCOM_OVERVIEW")[0])
    except:
        print("error")

这是输出

the value of page 2 is: /Hotels-g60713-oa30-San_Francisco_California-Hotels.html
the value of page 3 is: /Hotels-g60713-oa60-San_Francisco_California-Hotels.html
the value of page 4 is: /Hotels-g60713-oa90-San_Francisco_California-Hotels.html
the value of page 5 is: /Hotels-g60713-oa120-San_Francisco_California-Hotels.html
the value of page 6 is: /Hotels-g60713-oa150-San_Francisco_California-Hotels.html
the value of page 8 is: /Hotels-g60713-oa210-San_Francisco_California-Hotels.html

注意 url 中的 -oa###-。这可以更改,您可以获得所有后续页面。