【问题标题】:retrieve all car links from dynamic page从动态页面检索所有汽车链接
【发布时间】:2019-03-14 16:40:20
【问题描述】:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-agent='Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36'")
#options.add_argument("headless")
driver=webdriver.Chrome(executable_path="/home/timmy/Python/chromedriver",chrome_options=options)

url="https://turo.com/search?country=US&defaultZoomLevel=7&endDate=03%2F20%2F2019&endTime=10%3A00&international=true&isMapSearch=false&itemsPerPage=200&location=Colorado%2C%20USA&locationType=City&maximumDistanceInMiles=30&northEastLatitude=41.0034439&northEastLongitude=-102.040878&region=CO&sortType=RELEVANCE&southWestLatitude=36.992424&southWestLongitude=-109.060256&startDate=03%2F15%2F2019&startTime=10%3A00"
driver.get(url)


list_of_all_car_links=[]
x=0
while True:
    html=driver.page_source
    soup = BeautifulSoup(html, "html.parser")
    for i in soup.find_all("a", href=True):
        if i['href'].startswith("/rentals") and len(i['href']) > 31 :
            link2="https://turo.com"+i['href']
            list_of_all_car_links.append(link2)
    try:
        x=scrolldown(last_height=x)
    except KeyError:
        #driver.close()
        break

我尝试向下滚动然后查找链接,但我只得到了部分是我的向下滚动功能:

def scrolldown(last_height=0,SCROLL_PAUSE_TIME=3,num_tries = 2):

        # Scroll down to bottom
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")

        # Wait to load page
    time.sleep(SCROLL_PAUSE_TIME)

    new_height = driver.execute_script("return document.body.scrollHeight")

        # break condition
    if last_height == new_height:
        #print("hello")
        num_tries-=1
        if num_tries==0:
            print("Reached End of page")
            raise KeyError
        else:
            scrolldown(last_height=last_height, SCROLL_PAUSE_TIME=2,num_tries=num_tries)

    return new_height

我还尝试在每次滚动后将 html 转换为 BeautifulSoup 然后找到链接但没有得到所有链接。

我想要的是获取该页面中的每个汽车链接。

【问题讨论】:

  • 您是否签出请求 url 以 https://turo.com/api/search 开头。它包含您想要的一切(至少 200 项)。我在其中找到了车辆网址。

标签: python web-scraping


【解决方案1】:

我会使用requests 和开发工具中 xhr 列表中显示的 API。请注意查询字符串itemsPerPage=200 中的每页项目参数。您可以尝试将其更改为更大的结果集。

import requests
url = 'https://turo.com/api/search?country=US&defaultZoomLevel=7&endDate=03%2F20%2F2019&endTime=10%3A00&international=true&isMapSearch=false&itemsPerPage=200&location=Colorado%2C%20USA&locationType=City&maximumDistanceInMiles=30&northEastLatitude=41.0034439&northEastLongitude=-102.040878&region=CO&sortType=RELEVANCE&southWestLatitude=36.992424&southWestLongitude=-109.060256&startDate=03%2F15%2F2019&startTime=10%3A00'
baseUrl = 'https://turo.com'
headers = {'Referer' : 'https://turo.com/search?country=US&defaultZoomLevel=7&endDate=03%2F20%2F2019&endTime=10%3A00&international=true&isMapSearch=false&itemsPerPage=200&location=Colorado%2C%20USA&locationType=City&maximumDistanceInMiles=30&northEastLatitude=41.0034439&northEastLongitude=-102.040878&region=CO&sortType=RELEVANCE&southWestLatitude=36.992424&southWestLongitude=-109.060256&startDate=03%2F15%2F2019&startTime=10%3A00',
           'User-Agent' : 'Mozilla/5.0'}

r = requests.get(url, headers = headers).json()
results = []

for item in r['list']:
    results.append(baseUrl + item['vehicle']['url'])
print(results)

【讨论】:

  • 谢谢,这正好提供了 200 个链接,我如何确定这是整个列表?
  • 每页有一个项目参数,您可以在 url itemsPerPage=200 中更改
  • 即使我改了长度还是200
  • 我怀疑这是每页的最大值。检查时页面上是否有超过 200 个可用?是否可以在 url 中传递分页参数?例如页 = 1
  • 您目前只能在页面本身上获得视口中的结果(例如使用 css 选择器)您可以尝试滚动直到所有内容都可见并手动计数以确认。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 2017-04-08
  • 2020-02-29
相关资源
最近更新 更多