【问题标题】:How to scrape dynamic webpages by Python如何通过 Python 抓取动态网页
【发布时间】:2016-02-21 02:49:43
【问题描述】:

[我要做什么]

在下面的网页上抓取二手车数据。
http://www.goo-net.com/php/search/summary.php?price_range=&pref_c=08,09,10,11,12,13,14&easysearch_flg=1

[问题]

抓取整个页面。在上面的 url 中,只显示了前 30 个项目。这些可以被我写的下面的代码刮掉。到其他页面的链接显示为 1 2 3...,但链接地址似乎是 Javascript。我用谷歌搜索有用的信息,但找不到任何信息。

from bs4 import BeautifulSoup
import urllib.request

html = urllib.request.urlopen("http://www.goo-net.com/php/search/summary.php?price_range=&pref_c=08,09,10,11,12,13,14&easysearch_flg=1")

soup = BeautifulSoup(html, "lxml")
total_cars = soup.find(class_="change change_01").find('em').string
tmp = soup.find(class_="change change_01").find_all('span')
car_start, car_end = tmp[0].string, tmp[1].string

# get urls to car detail pages
car_urls = []
heading_inners = soup.find_all(class_="heading_inner")
for heading_inner in heading_inners:
    href = heading_inner.find('h4').find('a').get('href')
    car_urls.append('http://www.goo-net.com' + href)

for url in car_urls:
    html = urllib.request.urlopen(url)
    soup = BeautifulSoup(html, "lxml")
    #title
    print(soup.find(class_='hdBlockTop').find('p', class_='tit').string)
    #price of car itself
    print(soup.find(class_='price1').string)
    #price of car including tax
    print(soup.find(class_='price2').string)

    tds = soup.find(class_='subData').find_all('td')
    # year
    print(tds[0].string)
    # distance
    print(tds[1].string)
    # displacement
    print(tds[2].string)
    # inspection
    print(tds[3].string)

[我想知道的]

如何抓取整个页面。我更喜欢使用 BeautifulSoup4 (Python)。但如果这不是合适的工具,请告诉我其他的。

[我的环境]

  • Windows 8.1
  • Python 3.5
  • PyDev (Eclipse)
  • BeautifulSoup4

任何指导将不胜感激。谢谢。

【问题讨论】:

    标签: python html web-scraping beautifulsoup scrape


    【解决方案1】:

    您可以使用selenium 如下示例:

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get('http://example.com')
    element = driver.find_element_by_class_name("yourClassName") #or find by text or etc
    element.click() 
    

    【讨论】:

    • @dixhom,请随时单击任何接近回答问题的答案附近的勾号。在 StackOverflow 上接受答案的可靠历史将鼓励更多人回答您后续的问题。
    • 嗨..你觉得你能帮我stackoverflow.com/questions/43033378/…
    【解决方案2】:

    python 模块splinter 可能是一个很好的起点。它调用外部浏览器(例如 Firefox)并访问浏览器的 DOM,而不是只处理 HTML。

    【讨论】:

    • 感谢您的回答。这是我第一次了解 DOM,我可以让它做诸如“选择这个元素”和“点击那个元素”之类的事情?现在我正在阅读 splinter 网站。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2021-05-07
    • 1970-01-01
    • 2020-06-21
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多