如何使用美丽汤从页面中提取链接

时间:2019-06-03 04:26:56

标签: python beautifulsoup

我有一个HTML Page,具有多个div,例如:

<div class="post-info-wrap">
  <h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-1st-post/" title="Example of 1st post &#8211; Example 1 Post" rel="bookmark">sample post &#8211; example 1 post</a></h2>
  <div class="post-meta clearfix">

    <div class="post-info-wrap">
      <h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-2nd-post/" title="Example of 2nd post &#8211; Example 2 Post" rel="bookmark">sample post &#8211; example 2 post</a></h2>
      <div class="post-meta clearfix">

我需要使用class post-info-wrap获取所有div的值,我是BeautifulSoup的新手

所以我需要这些网址:

我尝试过:

import re
import requests
from bs4 import BeautifulSoup

r = requests.get("https://www.example.com/blog/author/abc") 
data = r.content  # Content of response

soup = BeautifulSoup(data, "html.parser")
for link in soup.select('.post-info-wrap'):
   print link.find('a').attrs['href']

此代码似乎无效。我是美丽汤的新手。如何提取链接?

2 个答案:

答案 0 :(得分:3)

您可以使用soup.find_all

from bs4 import BeautifulSoup as soup
r = [i.a['href'] for i in soup(html, 'html.parser').find_all('div', {'class':'post-info-wrap'})]

输出:

['https://www.example.com/blog/111/this-is-1st-post/', 'https://www.example.com/blog/111/this-is-2nd-post/']

答案 1 :(得分:1)

link = i.find('a',href=True)始终不返回anchor tag (a),可能返回NoneType,因此您需要验证链接是否为None,继续循环,否则获取链接href值。

按网址取消链接:

import re
import requests
from bs4 import BeautifulSoup
r = requests.get("https://www.example.com/blog/author/abc")
data = r.content  # Content of response
soup = BeautifulSoup(data, "html.parser")

for i in soup.find_all('div',{'class':'post-info-wrap'}):
   link = i.find('a',href=True)
   if link is None:
       continue
   print(link['href'])

通过HTML取消链接:

from bs4 import BeautifulSoup
html = '''<div class="post-info-wrap"><h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-1st-post/" title="Example of 1st post &#8211; Example 1 Post" rel="bookmark">sample post &#8211; example 1 post</a></h2><div class="post-meta clearfix">
<div class="post-info-wrap"><h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-2nd-post/" title="Example of 2nd post &#8211; Example 2 Post" rel="bookmark">sample post &#8211; example 2 post</a></h2><div class="post-meta clearfix">'''

soup = BeautifulSoup(html, "html.parser")

for i in soup.find_all('div',{'class':'post-info-wrap'}):
   link = i.find('a',href=True)
   if link is None:
       continue
   print(link['href'])

更新

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get("https://www.example.com/blog/author/abc")

soup = BeautifulSoup(driver.page_source, "html.parser")

for i in soup.find_all('div', {'class': 'post-info-wrap'}):
    link = i.find('a', href=True)
    if link is None:
        continue
    print(link['href'])

O / P:

https://www.example.com/blog/911/article-1/
https://www.example.com/blog/911/article-2/
https://www.example.com/blog/911/article-3/
https://www.example.com/blog/911/article-4/
https://www.example.com/blog/random-blog/article-5/

对于Chrome浏览器:

http://chromedriver.chromium.org/downloads

为Chrome浏览器安装Web驱动程序:

https://christopher.su/2015/selenium-chromedriver-ubuntu/

硒教程

https://selenium-python.readthedocs.io/

'/usr/bin/chromedriver' chrome浏览器驱动程序的路径。