【发布时间】:2020-05-07 23:12:02
【问题描述】:
我有一个有 3 个主题的论坛。我正在尝试抓取所有三个帖子中的数据。所以我需要按照每个帖子的href链接并抓取数据。这给了我一个错误,我不确定我错了什么......
import csv
import time
from bs4 import BeautifulSoup
import requests
source = requests.get('https://mainforum.com').text
soup = BeautifulSoup(source, 'lxml')
#get the thread href (thread_link)
for threads in soup.find_all('p', class_= 'small'):
thread_name = threads.text
thread_link = threads.a.get('href')# there are three threads and this gets all 3 links
print (thread_link)
其余代码是我遇到问题的地方?
# request the individual thread links
for follow_link in thread_link:
response = requests.get(follow_link)
#parse thread link
soup= BeautifulSoup(response, 'lxml')
#print Data
for p in soup.find_all('p'):
print(p)
【问题讨论】:
-
亲爱的 Blake - 如果您发布完整的代码,这将有助于完全理解和掌握。这可能会帮助(尤其是我)在这里所有学习的人扩展见解和理解。 - 提前致谢 - 你的零
-
@zero 你是什么意思?我错过了什么吗?
-
它是否成功导航到其他链接?如果打印整个 html 文档会发生什么?
-
@TenaciousB 不,任何链接都不行...你做一次你得到它......我可以打印href很好(代码的顶部),就是这样......我几乎用那个循环写了每个链接,这可能有点问题,但有些我可以稍后处理......我现在需要的是它至少导航到其中一个链接......我得到的错误是:requests.exceptions.MissingSchema:无效的 URL 'h':没有提供架构。也许你的意思是 http://h?
-
您可能在
response = requests.get(follow_link)中缺少.text
标签: python beautifulsoup