我想在返回的 http header 中找到一个 url。根据beautiful soup有一种方法可以使用 soup.find_all(re.compile("yourRegex")
收集数组中的正则表达式匹配项。但是,我的正则表达式中肯定缺少一些内容,它在 text editor that I am using 的正则表达式查找中匹配。 ,但与以下代码内部不匹配:
从 bs4 导入 BeautifulSoup 导入请求 进口重新 导入 csv 导入 json 导入时间 导入文件输入 导入 urllib2
data = urllib2.urlopen("http://stackoverflow.com/questions/16627227/http-error-403-in-python-3-web-scraping").read()
soup = BeautifulSoup(data)
stringSoup = str(soup)
#Trying to use compile
print soup.find_all(re.compile("[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?"))
我尝试过输入 ()
围绕正则表达式,以及以 r
开头...我缺少什么是必要的?
我也一直在使用http://www.pythonregex.com/ ,投入[a-zA-Z0-9\-\.]+\.(com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
正则表达式部分中包含 url,另一部分中包含 url,但那里也没有匹配项。
谢谢!
最佳答案
print re.findall(r"[a-zA-Z0-9\-\.]+\.(?:com|org|net|mil|edu|COM|ORG|NET|MIL|EDU)+(?:[\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?",x)
试试这个。这对我有用。
x="""<!DOCTYPE html>
<html itemscope itemtype="http://schema.org/QAPage">
<head>
"""
输出:schema.org/QAPage
关于python - 使用 python re.compile 和 beautiful soup 来匹配字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26534801/