我想在通过python中的BeautifulSoup
库获取链接的HTML之后提取其标题。
基本上,整个标题标签是
<title>Imaan Z Hazir on Twitter: "Guantanamo and Abu Ghraib, financial and military support to dictators in Latin America during the cold war. REALLY, AMERICA? (3)"</title>
我想提取"标记中仅此Guantanamo and Abu Ghraib, financial and military support to dictators in Latin America during the cold war. REALLY, AMERICA? (3)
中的数据
我试着
我也试过
for i in data.find_all("title.""):
for i in data.find_all("title>""):
for i in data.find_all("""):
以及
for i in data.find_all("quot"):
但是没有人在工作。在
解析完html后:
按以下方式查找标题:
^{pr2}${cd1>找到两个引号。有很多方法可以做到这一点。我会使用正则表达式:
您永远不会搜索
"
或任何其他&NAME;
序列,因为beautifulGroup会将它们转换为它们表示的实际字符。在编辑:
不捕获引号的Regex将是:
下面是一个使用正则表达式提取引号内文本的简单完整示例:
这里的情况是,在获取页面的源代码并找到
title
之后,我们对标题使用正则表达式来提取引号中的文本。在我们告诉正则表达式在开始引号(
\"
)之前的字符串开头(^.*
)查找任意数量的符号,然后捕获它和右引号(第二个\"
)之间的文本。在然后,我们通过告诉Python打印第一个捕获的组(regex中括号之间的部分)来打印捕获的文本。在
下面是关于在python中匹配regex的更多信息-https://docs.python.org/3/library/re.html#match-objects
只需在冒号上拆分文本:
实际上,查看一点也不需要拆分的页面,文本位于内的p标记中分区js-tweet文本容器,th:
^{pr2}$所以你可以用任何一种方法得到同样的结果。在
相关问题 更多 >
编程相关推荐