【发布时间】:2021-04-23 12:16:08
【问题描述】:
我一直在尝试随机化维基百科页面并获取该随机站点的 URL。尽管我可以获取网站上的每个链接,但由于某种原因,我无法访问这段 html 代码并获取 href。
一个随机的维基百科页面示例。
<a accesskey="v" href="https://en.wikipedia.org/wiki/T%C5%99eb%C3%ADvlice?action=edit" class="oo-ui-element-hidden"></a>
所有的维基百科页面都有这个,我需要得到href,这样我才能以一种可以获得当前 URL 的方式操作它。
到目前为止我写的代码:
from bs4 import BeautifulSoup
import requests
links = []
for x in range(0, 1):
source = requests.get("https://en.wikipedia.org/wiki/Special:Random").text
soup = BeautifulSoup(source, "lxml")
print(soup.find(id="firstHeading"))
for link in soup.findAll('a'):
links.append(link.get('href'))
print(links)
直接获取当前 URL 也会有所帮助,但是我在网上找不到解决方案。
我也在使用 Lunix 操作系统——如果有帮助的话——
【问题讨论】:
-
帮助我们为您提供帮助 - 请显示您的代码并改进您的问题,以便我们重现您的问题。 How to create a Minimal, Reproducible Example谢谢
标签: python python-3.x url web-scraping beautifulsoup