Python请求无法获取网站的所有链接

发布于 2025-01-11 10:09:39 字数 602 浏览 2 评论 0原文

我正在学习如何使用 Python urllib.requests 模块,并且我一直在尝试从网站获取所有链接,尽管它适用于大多数链接,但我在打开 这个

我为此链接获得的输出只是: # python teosa.py []

整个代码如下所示:

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request("https://randomtube.xyz/")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

links = []
for link in soup.findAll('a'):
    links.append(link.get('href'))

print(links) 

有谁知道可能是什么问题?

I'm learning how to use Python urllib.requests module and I've been trying to get all the links from the website, although it works for most of them, I'm having trouble opening this one.

The output I'm getting for this link is just: # python teosa.py []

The whole code looks like this:

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request("https://randomtube.xyz/")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

links = []
for link in soup.findAll('a'):
    links.append(link.get('href'))

print(links) 

Does anyone know what could be the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖公主 2025-01-18 10:09:39

问题是 url 中没有给出链接。

您的脚本应该按原样运行。尝试一个带有链接的网站。

注意:我认为您不需要“导入重新”。

#!/usr/bin/python3
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
#import re

req = Request("https://google.com/")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

links = []
for link in soup.findAll('a'):
    links.append(link.get('href'))

print(links)
['https://www.google.com/imghp?hl=en&tab=wi', 'https://maps.google.com/maps?hl=en&tab=wl', 'https://play.google.com/?hl=en&tab=w8', 'https://www.youtube.com/?gl=US&tab=w1', 'https://news.google.com/?tab=wn', 'https://mail.google.com/mail/?tab=wm', 'https://drive.google.com/?tab=wo', 'https://www.google.com/intl/en/about/products?tab=wh', 'http://www.google.com/history/optout?hl=en', '/preferences?hl=en', 'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/&ec=GAZAAQ', '/advanced_search?hl=en&authuser=0', '/intl/en/ads/', '/services/', '/intl/en/about.html', '/intl/en/policies/privacy/', '/intl/en/policies/terms/']

The issue is there are no links in url given.

Your script should work as-is. Try a site with links.

Note: I do not think you need to 'import re'.

#!/usr/bin/python3
from urllib.request import Request, urlopen
from bs4 import BeautifulSoup
#import re

req = Request("https://google.com/")
html_page = urlopen(req)

soup = BeautifulSoup(html_page, "lxml")

links = []
for link in soup.findAll('a'):
    links.append(link.get('href'))

print(links)
['https://www.google.com/imghp?hl=en&tab=wi', 'https://maps.google.com/maps?hl=en&tab=wl', 'https://play.google.com/?hl=en&tab=w8', 'https://www.youtube.com/?gl=US&tab=w1', 'https://news.google.com/?tab=wn', 'https://mail.google.com/mail/?tab=wm', 'https://drive.google.com/?tab=wo', 'https://www.google.com/intl/en/about/products?tab=wh', 'http://www.google.com/history/optout?hl=en', '/preferences?hl=en', 'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/&ec=GAZAAQ', '/advanced_search?hl=en&authuser=0', '/intl/en/ads/', '/services/', '/intl/en/about.html', '/intl/en/policies/privacy/', '/intl/en/policies/terms/']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文