繁体   English   中英

Python正则表达式匹配线如果结束?

[英]Python Regex Match Line If Ends With?

这是我试图刮:

        <p>Some.Title.html<br />
<a href="https://www.somelink.com/yep.html" rel="nofollow">https://www.somelink.com/yep.html</a><br />
Some.Title.txt<br />
<a href="https://www.somelink.com/yeppers.txt" rel="nofollow">https://www.somelink.com/yeppers.txt</a><br />

我尝试了以下几种变体:

match = re.compile('^(.+?)<br \/><a href="https://www.somelink.com(.+?)">',re.DOTALL).findall(html)

我希望匹配线条与“p”标签,没有。 “p”标记仅出现在第一个实例上。 在python很可怕,所以我很生疏,在这里搜索和google,似乎没有什么是相同的。 谢谢你的帮助。 真的很感谢我遇到困难时得到的帮助。

期望的输出是一个索引:

<a href="Some.Title.html">http://www.SomeLink.com/yep.html</a>
<a href="Some.Title.txt">http://www.SomeLink.com/yeppers.txt</a>

使用美丽的汤和请求模块将是这样的事情的完美,而不是像上面提到的评论者那样的正则表达式。

import requests
import bs4

html_site = 'www.google.com' #or whatever site you need scraped
site_data = requests.get(html_site) # downloads site into a requests object
site_parsed = bs4.BeautifulSoup(site_data.text) #converts site text into bs4 object
a_tags = site_parsed.select('a') #this will select all 'a' tags and return list of them

这只是一个简单的代码,它将从html网站中选择所有标签,并将它们存储在上面列出的格式的列表中。 我建议在这里查看bs4上的一个很好的教程, 这里是实际的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM