【问题标题】:How to extract href when ['href'] element is a hyperlink当['href']元素是超链接时如何提取href
【发布时间】:2020-01-08 06:56:12
【问题描述】:

我正在尝试从网页中抓取数据,然后通过将 href 提取到下一页来转到下一页。

但是,在这种情况下,包含指向下一页的 href 的标记是 href='#next'。 在用 Chrome 检查这个元素时,当我将鼠标悬停在“#next”这个词上时,它似乎是一个向我显示完整 href 的超链接。

我怀疑一旦我提出请求并将其转换为这样的文本,href 就会丢失;

r = requests.get(url)

s = BeautifulSoup(r.text)

我使用findAll() 函数来获取我正在寻找的元素:

s.findAll('a', class_='pagenav')[5]

结果:

a href="#next" class="pagenav" title="next page" onclick="javascript:
document.pageForm.limitstart.value=20; document.pageForm.submit();return false;">
Next >

在这种情况下如何获取 href?

这是网站的链接

https://associatedrealtorsaruba.com/index.php?option=com_ezrealty&Itemid=11&task=results&cnid=0&custom7=&custom8=&parking=&type=0&cid=0&stid=0&locid=0&minprice=&maxprice=&minbed=&maxbed=&min_squarefeet=&max_squarefeet=&bathrooms=&sold=0&lug=0&featured=0&custom4=&custom5=&custom6=&postcode=&radius=&direction=DEFAULT&submit=Search

【问题讨论】:

  • 您无法使用 href value 进行导航。因为它是 javascripts onclick 事件。但是,如果您的网址是公开的,您可以分享,以便 OP 可以帮助您。
  • JavaScript 可以向该元素添加 url 但 BeautifulSoup 无法运行 JavaScript - 您可能需要 Selenium 来控制可以运行 JavaScript 的 Web 浏览器
  • 如果此页面使用带有pageForm 的JavaScript,那么URL 可能位于<form action="url"> 标记中
  • @furas 我尝试使用 Selenium 导入 webdriver 来运行 Firefox(),然后使用 .execute_script("return document.documentElement.outerHTML") 来获取 javascript 后面的 html。但是我使用 .find('a', class_='pagenav') 得到了相同的结果。我确实注意到新的 html 中出现了新的 标签: class="prevLink",但是,它是 href='#'
  • 如果您使用Selenium,则使用Selenium 查找<a class="pagenav"><a title="next page">.click() 以加载下一页,您不必为此获取href .

标签: python beautifulsoup onclick href


【解决方案1】:

如果您使用Selenium,则使用Selenium 查找<a class="pagenav"><a title="next page">.click() 以加载下一页,您不必为此获取href

import selenium.webdriver

url = 'https://associatedrealtorsaruba.com/index.php?option=com_ezrealty&Itemid=11&task=results&cnid=0&custom7=&custom8=&parking=&type=0&cid=0&stid=0&locid=0&minprice=&maxprice=&minbed=&maxbed=&min_squarefeet=&max_squarefeet=&bathrooms=&sold=0&lug=0&featured=0&custom4=&custom5=&custom6=&postcode=&radius=&direction=DEFAULT&submit=Search'

driver = selenium.webdriver.Firefox()
driver.get(url)

# find link to next page
next_page = driver.find_element_by_xpath('//a[@title="next page"]')

# click link to load next page
next_page.click()

顺便说一句:如果您手动加载第 1、2 和 3 页并在浏览器中比较它们的 url,那么您将看到 url 的唯一区别

for page 1: &limitstart=0 
for page 2: &limitstart=20 
for page 3: &limitstart=40 

这是在不获取href 的情况下加载下一页的方法 - 您必须获取原始网址并添加具有正确值的&limitstart= 才能加载不同的页面。


如果要在页面上显示 50 个项目,则必须使用 &limit=50,然后 &limitstart 必须使用值 0、50、100 等。


编辑:

有请求

import requests
from bs4 import BeautifulSoup as BS

url = 'https://associatedrealtorsaruba.com/index.php?option=com_ezrealty&Itemid=11&task=results&cnid=0&custom7=&custom8=&parking=&type=0&cid=0&stid=0&locid=0&minprice=&maxprice=&minbed=&maxbed=&min_squarefeet=&max_squarefeet=&bathrooms=&sold=0&lug=0&featured=0&custom4=&custom5=&custom6=&postcode=&radius=&direction=DEFAULT&submit=Search'

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0', # need full UA
}

for x in (0, 20, 40):
    r = requests.get(url + '&limitstart={}'.format(x), headers=headers)
    print('\n---', x, '---\n')

    soup = BS(r.text, 'html.parser')

    all_items = soup.find_all('span', {'class': 'h3'})
    for item in all_items:
        print(item.get_text(strip=True))

import selenium.webdriver

url = 'https://associatedrealtorsaruba.com/index.php?option=com_ezrealty&Itemid=11&task=results&cnid=0&custom7=&custom8=&parking=&type=0&cid=0&stid=0&locid=0&minprice=&maxprice=&minbed=&maxbed=&min_squarefeet=&max_squarefeet=&bathrooms=&sold=0&lug=0&featured=0&custom4=&custom5=&custom6=&postcode=&radius=&direction=DEFAULT&submit=Search'

driver = selenium.webdriver.Firefox()
driver.get(url)

while True:

    all_items = driver.find_elements_by_xpath('//span[@class="h3"]')
    for item in all_items:
        print(item.text)

    try:    
        # find link to next page
        all_items = driver.find_element_by_xpath('//a[@title="next page"]')

        # click link to load next page
        all_items.click()
    except Exception as ex:
        print('ex:', ex)
        break

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 2018-08-17
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    相关资源
    最近更新 更多