我试图从一个特定的文件管理器中提取一些链接watchseriesfree.to网站。在下面的例子中,我想要rapidvideo链接,所以我使用regex过滤掉那些包含rapidvideo文本的标记
import re
import urllib2
from bs4 import BeautifulSoup
def gethtml(link):
req = urllib2.Request(link, headers={'User-Agent': "Magic Browser"})
con = urllib2.urlopen(req)
html = con.read()
return html
def findLatest():
url = "https://watchseriesfree.to/serie/Madam-Secretary"
head = "https://watchseriesfree.to"
soup = BeautifulSoup(gethtml(url), 'html.parser')
latep = soup.find("a", title=re.compile('Latest Episode'))
soup = BeautifulSoup(gethtml(head + latep['href']), 'html.parser')
firstVod = soup.findAll("tr",text=re.compile('rapidvideo'))
return firstVod
print(findLatest())
但是,上面的代码返回一个空白列表。我做错什么了?在
问题在于:
当} attribute 值。现在,
BeautifulSoup
将应用文本regex模式时,它将使用所有匹配的tr
元素的^{.string
有一个重要的警告-当一个元素有多个子元素时,.string
是None
:因此,您没有结果。在
您可以通过使用searching function并调用
^{pr2}$.get_text()
来检查tr
元素的实际文本:相关问题 更多 >
编程相关推荐