python - 提取并格式化站点数据 Python

标签 python html web-scraping

这适用于 Python 3.5.x 我正在寻找的是在一段 HTML 代码之后找到标题

<h3 class = "title-link__title"><span class="title=link__text">News Here</span>

with urllib.request.urlopen('http://www.bbc.co.uk/news') as r:
    HTML = r.read()
    HTML = list(HTML)
    for i in range(len(HTML)):
        HTML[i] = chr(HTML[i])

我怎样才能得到它,所以我只提取标题,因为这就是我所需要的。我会尽我所能尽力提供详细信息。

最佳答案

从网页获取信息称为网络抓取

完成这项工作的最佳工具之一是 BeautifulSoup图书馆。

from bs4 import BeautifulSoup
import urllib

#opening page
r = urllib.urlopen('http://www.bbc.co.uk/news').read()
#creating soup
soup = BeautifulSoup(r)

#useful for understanding the layout of your page info
#print soup.prettify()

#creating a ResultSet with all h3 tags that contains a class named 'title-link__title'
a = soup.findAll("h3", {"class":"title-link__title"})

#counting ocurrences
len(a)
#result = 44

#get text of first header
a[0].text
#result = u'\nMay v Leadsom to be next UK PM\n'

#get text of second header
a[1].text
#result = u'\nVideo shows US police shooting aftermath\n'

关于python - 提取并格式化站点数据 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254553/

相关文章:

python - 页面源不显示 selenium/Python 的广告

Python - Tkinter 标签输出?

python - Sphinx - 从父方法插入参数文档

python - Pandas - 如何在函数中传递列名称

javascript - 如何使用 JavaScript 或 jQuery 让我的 "input a word"脚本正常工作?

python - 如何限制scrapy请求对象?

python - 是否可以将已编译的 .pyc 文件反编译为 .py 文件?

python - lxml.html 解析和带有请求的 utf-8

javascript - 在 iPad 上使用 Javascript 加载大图像和图 block

python - 将抓取数据转储到MySQL数据库中