Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按照论坛中的链接使用BS4刮线程(注释)_Python_Beautifulsoup - Fatal编程技术网

Python 按照论坛中的链接使用BS4刮线程(注释)

Python 按照论坛中的链接使用BS4刮线程(注释),python,beautifulsoup,Python,Beautifulsoup,我有一个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 hr

我有一个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)

至于您的架构错误…

您得到架构错误,因为您正在一次又一次地覆盖一个链接。然后尝试调用该链接,就好像它是一个链接列表一样。此时,它是一个字符串,您只需遍历字符(从
h
开始),因此会出现错误

请看这里:


关于一般查询以及如何解决类似问题…

如果我这样做,流程将如下所示:

  • 获取三个HREF(类似于您已经完成的操作)
  • 使用一个函数,该函数将逐个刮取线程HREF,并返回您希望它们返回的任何内容
  • 将返回的信息保存/附加到任意位置
  • 重复

  • 像这样的事情可能

    导入csv
    导入时间
    从bs4导入BeautifulSoup
    导入请求
    source=请求。get('https://mainforum.com')
    soup=BeautifulSoup(source.content,“lxml”)
    所有线程信息=[]
    def刮线链接(href):
    response=requests.get(href)
    #解析线程链接
    汤=BeautifulSoup(response.content,“lxml”)
    #返回数据
    返回[p.text表示汤中的p.find_all('p')]
    #获取线程href(线程链接)
    对于汤中的线程。查找所有('p',类='small'):
    此线程信息={}
    此线程信息[“线程名称”]=threads.text
    此线程信息[“线程链接”]=threads.a.get('href')
    此线程信息[“线程数据”]=刮取线程链接(此线程信息[“线程链接”])
    所有线程信息。追加(此线程信息)
    打印(所有线程信息)
    

    在最初的问题中有很多未明确的地方,所以我做了一些假设。理想情况下,尽管你能看到要点


    另请注意,我更喜欢使用
    .content
    响应,而不是
    。text

    all_thread_info = []
    
    def scrape_thread_link(href):
        response = requests.get(href)
        soup= BeautifulSoup(response.content, 'lxml')
    
        for Thread in soup.find_all(id= 'discussionReplies'):
            Thread_Name = Thread.find_all('div', class_='xg_user_generated')
            for Posts in Thread_Name:
                print(Posts.text)
    
    
    for threads in soup.find_all('p', class_= 'small'):
        thread_name = threads.text
        thread_link = threads.a.get('href')
        thread_data = scrape_thread_link(thread_link)
        all_thread_info.append(thread_data)
    

    亲爱的布莱克-如果你能发布完整的代码,那么完全理解并掌握它会很有帮助。这可能有助于(尤其是我)这里所有的学习者扩展洞察力和理解力thx预付款-您的zero@zero什么意思?我是否遗漏了什么?它是否成功导航到其他链接?如果你打印整个html文档会发生什么?@坚韧B没有任何链接。。。tbh。。。我从来没有做过与BS4的链接导航。。。大多数指南都会告诉您如何获取href,但不会告诉您在获取href后要做什么。。。我可以打印href fine(代码的顶部),就是这样。。。我几乎是在写每一个循环的链接,这可能是一个小问题,但一些我可以处理后。。。我现在需要的是它至少导航到一个链接。。。我收到的错误是:requests.exceptions.MissingSchema:无效URL“h”:未提供架构。也许您的意思是http://h?您可能缺少
    response=requests.get(follow_link)
    中的
    .text
    ,嘿,谢谢,它似乎在工作,做了一些调整。使用1个链接和3个链接。希望您能给出我是否还可以的反馈?您在scrape\u thread\u link函数中缺少一条返回语句。我想您应该创建一个列表,并将
    Posts.text
    附加到该列表中。然后您可以返回该列表。然后您将使用
    all\u thread\u info.extend(thread\u data)
    而不是
    append
    。同样值得注意的是,您应该在第一个字母大写的地方命名变量。有关命名约定的更多信息,请参见此处-如果这没有意义,请提出另一个问题,并将该问题的链接发布为对此评论的答复。我会回答这个新问题。另外,你不应该把额外的问题作为你原来问题的“答案”发布。他们最终会被我的版主删除。只要创建一个新问题,你就可以在评论中引用它。