当前位置: 首页 > 面试题库 >

如何通过Python抓取动态网页

梁鸣
2023-03-14
问题内容

[我想做的事]

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

[问题]

刮整个页面。在上面的网址中,仅显示前30个项目。我可以在下面编写的代码中删除这些内容。指向其他页面的链接显示为1 2 3
…,但是链接地址似乎在Javascript中。我在Google上搜索了有用的信息,但找不到任何信息。

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)
  • 美丽的汤4

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


问题答案:

您可以像下面的示例一样使用硒:

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()


 类似资料:
  • 问题内容: 在网站上,有在标顶部的几个环节,,,和。如果按下以数字标记的链接,它将动态地将一些数据加载到content中。如果被按下,它会用标签页,,,和第4页中的数据显示。 我想从按下的所有链接的内容中抓取数据(我不知道有多少,一次只显示3个,然后) 请举一个例子。例如,考虑网站www.cnet.com。 请指导我下载使用selenium的一系列页面,并自行解析它们以处理漂亮的汤。 问题答案:

  • 我尝试用BS4 python来抓取动态网站: https://www.nadlan.gov.il/?search=תל אביב יפו 我试过: 我有两个问题: > 当我打开站点时,数据加载需要几秒钟: 硒如何解决这些问题?

  • 本文向大家介绍python+selenium+PhantomJS抓取网页动态加载内容,包括了python+selenium+PhantomJS抓取网页动态加载内容的使用技巧和注意事项,需要的朋友参考一下 环境搭建 准备工具:pyton3.5,selenium,phantomjs 我的电脑里面已经装好了python3.5 安装Selenium pip3 install selenium 安装Phan

  • 我是python新手,正在尝试从以下站点获取数据。虽然这段代码适用于不同的站点,但我无法让它适用于nextgen stats。有人想知道为什么吗?下面是我的代码和我得到的错误 下面是我得到的错误 df11=pd。读取html(urlwk1)回溯(上次调用):文件“”,第1行,在文件“C:\Users\USERX\AppData\Local\Packages\PythonSoftwareFounda

  • 我正试图浏览一个网站。我尝试过使用两种方法,但都没有提供完整的网站源代码,我正在寻找。我正试图从下面提供的网站URL中获取新闻标题。 URL:"https://www.todayonline.com/" 这是我尝试过但失败的两种方法。 请帮忙。我试着抓取其他新闻网站,这要容易得多。谢谢你。

  • 我想使用Python在这样的网页上抓取“你在寻找这些作者吗”框的内容:http://academic.research.microsoft.com/Search?query=lander 不幸的是,该框的内容是由JavaScript动态加载的。通常在这种情况下,我可以阅读Javascript来了解发生了什么,或者我可以使用Firebug之类的浏览器扩展来了解动态内容的来源。这次没有这样的运气。。。