您好,似乎可以在大多数网页上使用,但我不明白为什么它不会带回该网站的链接。有谁知道为什么?查看源码,有这样的内容: 它应该带回“a href="https://thebillingtongroup.com/food-group/billington-foodservice/”。
import httplib2
from bs4 import BeautifulSoup, SoupStrainer
http = httplib2.Http()
status, response = http.request('https://thebillingtongroup.com/')
for link in BeautifulSoup(response, 'html.parser', parse_only=SoupStrainer('a')):
if link.has_attr('href'):
print(link['href'])
最佳答案
您需要使用请求 header :
headers = {'User-Agent': 'Mozilla/5.0'}
http = httplib2.Http()
status, response = http.request('https://thebillingtongroup.com/', headers=headers)
...
某些网站需要有效的User-Agent
来处理请求。
关于python - 特定网站不允许我从他们的页面获取链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59422550/