我在HTML中有类似这样的东西:
some text: (8/4)some other text, (9/4)
some text:, (19/6)text after comment, text i want
...我的Python代码:
page = requests.get(site)
soup = BeautifulSoup(page.content, 'html.parser')
rounds = soup.find('p', align="left")
matches_links = rounds.find_all('a')我得到了一些评论和文字之后的所有链接。之后我什么也得不到。这两个块引号在页面代码中是不可见的,只有在调试Python代码时才能在soup.。在soup我有所有的HTML代码,但是在rounds代码结束于text after commen..。
有没有办法获得“我想要的链接”和“我想要的文本”?
发布于 2020-08-19 18:14:18
如果你看一下HTML代码,你会发现有
之前。这意味着您的变量rounds不包含您想要的链接。搜索下一个
在这之后
标签:
from bs4 import BeautifulSoup
txt = '''
some text: (8/4)some other text, (9/4)
some text:, (19/6)text after comment, text i want
...
'''
soup = BeautifulSoup(txt, 'html.parser')
matched_link = soup.select_one('p[align="left"] ~ a')
print(matched_link)打印:
text i wanthttps://stackoverflow.com/questions/63484458
复制相似问题