【问题标题】:Python: search individual tag through BeautifulSoup [duplicate]Python:通过 BeautifulSoup 搜索单个标签 [重复]
【发布时间】:2016-11-13 22:05:28
【问题描述】:

我正在尝试创建一个来自 BBC 阅读量最高的部分的前 10 篇新闻文章列表。我的代码如下:

from bs4 import BeautifulSoup, SoupStrainer
import urllib2
import re

opener = urllib2.build_opener()

url = 'http://www.bbc.co.uk/news/popular/read'

soup = BeautifulSoup(opener.open(url), "lxml")

titleTag = soup.html.head.title

print(titleTag.string)

tagSpan = soup.find_all("span");

for tag in tagSpan:
    print(tag.get("class"))

我正在寻找的是<span class="most-popular-page-list-item__headline"></span> 之间的字符串

如何获取字符串并列出这些字符串?

【问题讨论】:

  • x = soup.find_all('span',{'class':'most-popular-page-list-item__headline'})
  • 太棒了,很高兴我能帮上忙!

标签: python python-2.7 web-scraping beautifulsoup


【解决方案1】:

这个怎么样:

from bs4 import BeautifulSoup
from urllib.request import urlopen

url = 'http://www.bbc.co.uk/news/popular/read'

page = urlopen(url)
soup = BeautifulSoup(page, "lxml")
titles = soup.findAll('span', {'class': "most-popular-page-list-item__headline"})
headlines = [t.text for t in titles]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2022-07-20
    • 1970-01-01
    相关资源
    最近更新 更多