【问题标题】:How to extract html links with a matching word from a website using python如何使用python从网站中提取带有匹配词的html链接
【发布时间】:2015-02-28 01:40:17
【问题描述】:

我有一个网址,比如http://www.bbc.com/news/world/asia/。就在这个页面中,我想提取所有包含 India 或 INDIA 或 india 的链接(应该不区分大小写)。

如果我点击任何输出链接,它应该会将我带到相应的页面,例如,这些是几行让印度印度对 Dhoni 退休感到震惊印度雾继续造成混乱。如果我点击这些链接,我应该分别被重定向到 http://www.bbc.com/news/world-asia-india-30640436http://www.bbc.com/news/world-asia-india-30630274

import urllib
from bs4 import BeautifulSoup
import re
import requests
url = "http://www.bbc.com/news/world/asia/"
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data)
only_links = SoupStrainer('a', href=re.compile('india'))
print (only_links)

我在 python 3.4.2 中编写了非常基本的最小代码。

【问题讨论】:

  • 您希望所有href 中包含india 作为子字符串。对吗?

标签: python html python-3.x beautifulsoup html-parsing


【解决方案1】:

您需要在显示的文本中搜索单词india。为此,您需要一个自定义函数:

from bs4 import BeautifulSoup
import requests

url = "http://www.bbc.com/news/world/asia/"
r = requests.get(url)
soup = BeautifulSoup(r.content)

india_links = lambda tag: (getattr(tag, 'name', None) == 'a' and
                           'href' in tag.attrs and
                           'india' in tag.get_text().lower())
results = soup.find_all(india_links)

india_links lambda 会查找所有标记为 <a> 的标记,链接具有 href 属性并在显示的文本中某处包含 india(不区分大小写)。

注意我使用了requests响应对象.content属性;将解码交给 BeautifulSoup!

演示:

>>> from bs4 import BeautifulSoup
>>> import requests
>>> url = "http://www.bbc.com/news/world/asia/"
>>> r = requests.get(url)
>>> soup = BeautifulSoup(r.content)
>>> india_links = lambda tag: getattr(tag, 'name', None) == 'a' and 'href' in tag.attrs and 'india' in tag.get_text().lower()
>>> results = soup.find_all(india_links)
>>> from pprint import pprint
>>> pprint(results)
[<a href="/news/world/asia/india/">India</a>,
 <a class="story" href="/news/world-asia-india-30647504" rel="published-1420102077277">India scheme to monitor toilet use </a>,
 <a class="story" href="/news/world-asia-india-30640444" rel="published-1420022868334">India to scrap tax breaks on cars</a>,
 <a class="story" href="/news/world-asia-india-30640436" rel="published-1420012598505">India shock over Dhoni retirement</a>,
 <a href="/news/world/asia/india/">India</a>,
 <a class="headline-anchor" href="/news/world-asia-india-30630274" rel="published-1419931669523"><img alt="A Delhi police officer with red flag walks amidst morning fog in Delhi, India, Monday, Dec 29, 2014. " src="http://news.bbcimg.co.uk/media/images/79979000/jpg/_79979280_79979240.jpg"/><span class="headline heading-13">India fog continues to cause chaos</span></a>,
 <a class="headline-anchor" href="/news/world-asia-india-30632852" rel="published-1419940599384"><span class="headline heading-13">Court boost to India BJP chief</span></a>,
 <a class="headline-anchor" href="/sport/0/cricket/30632182" rel="published-1419930930045"><span class="headline heading-13">India captain Dhoni quits Tests</span></a>,
 <a class="story" href="http://www.bbc.co.uk/news/world-radio-and-tv-15386555" rel="published-1392018507550"><img alt="A woman riding a scooter waits for a traffic signal along a street in Mumbai February 5, 2014." src="http://news.bbcimg.co.uk/media/images/72866000/jpg/_72866856_020889093.jpg"/>Special report: India Direct</a>,
 <a href="/2/hi/south_asia/country_profiles/1154019.stm">India</a>]

请注意此处的http://www.bbc.co.uk/news/world-radio-and-tv-15386555 链接;我们必须使用lambda 搜索,因为使用text 正则表达式的搜索不会找到该元素;包含的文本(Special report: India Direct)不是标签中的唯一元素,因此不会被找到。

/news/world-asia-india-30632852 链接也存在类似问题;嵌套的 &lt;span&gt; 元素使得 Court boost to India BJP Chief 标题文本不是链接标签的直接子元素。

你可以提取只是链接:

from urllib.parse import urljoin

result_links = [urljoin(url, tag['href']) for tag in results]

所有相对 URL 都相对于原始 URL 进行解析:

>>> from urllib.parse import urljoin
>>> result_links = [urljoin(url, tag['href']) for tag in results]
>>> pprint(result_links)
['http://www.bbc.com/news/world/asia/india/',
 'http://www.bbc.com/news/world-asia-india-30647504',
 'http://www.bbc.com/news/world-asia-india-30640444',
 'http://www.bbc.com/news/world-asia-india-30640436',
 'http://www.bbc.com/news/world/asia/india/',
 'http://www.bbc.com/news/world-asia-india-30630274',
 'http://www.bbc.com/news/world-asia-india-30632852',
 'http://www.bbc.com/sport/0/cricket/30632182',
 'http://www.bbc.co.uk/news/world-radio-and-tv-15386555',
 'http://www.bbc.com/2/hi/south_asia/country_profiles/1154019.stm']

【讨论】:

  • 嗨Martijin,感谢您的帮助。我看到了你提到的结果。我如何每隔 1 小时定期进行一次监控?
  • @sandy:这对于 cmets 来说太宽泛了。根据您的操作系统和其他要求,您可以采用多种方法来解决此问题,包括 cron 作业或 Windows 调度程序作业。
  • @sandy:发布一个新问题如果您无法在其他地方找到特定问题的答案
  • 忘记投票了,关于text 论点的要点。谢谢。
猜你喜欢
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多