【问题标题】:Python Scrape Forum for Title for each Post每个帖子标题的 Python Scrape 论坛
【发布时间】:2021-02-05 22:49:55
【问题描述】:

我是 Web 抓取的新手,也是 Python 的新手。我想在 URL 的论坛上抓取每个帖子的标题,然后创建一个新帖子,其中包含以下 1 个标题我想收到一封带有帖子链接的邮件。

通过搜索div structItem-title,我收到了 1 页上的 23 个帖子。但是当我想打印每个帖子的文本时,我只收到print(type(first_result.text))对于print(type(first_result)) .

搜索标题

    # Jeti_DS_16 = soup.find_all(text="Jeti DS 16")
    # Jeti_DS_16_v2 = soup.find_all(text="Jeti DS 16 2")
    # Jeti_DC_16 = soup.find_all(text="Jeti DC 16")
    # Jeti_DC_16_v2 = soup.find_all(text="Jeti DC 16 2")

代码

from requests import get
from bs4 import BeautifulSoup
import re
import smtplib
import time
import lxml


URL = 'https://www.rc-network.de/forums/biete-rc-elektronik-zubeh%C3%B6r.135/'

headers = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'}


def checkForSearchItem():

    response = get(URL)
    # print(response.text[:500])

    # page = requests.get(URL, headers=headers)
    # page = requests.get(URL, headers=headers).text
    # page = requests.get(URL).text
    # page = requests.get(URL)

    soup = BeautifulSoup(response.content, "lxml")
    # soup = BeautifulSoup(page.content, "html.parser")
    # soup = BeautifulSoup(page.text, "html.parser")

    search_for_class = soup.find_all(
        'div', class_='structItem-title')
    # search_for_main = soup.find_all(
    #     'div', class_="structItemContainer-group js-threadList")
    # Jeti_DS_16 = soup.find_all(text="Jeti DS 16")
    # Jeti_DS_16_v2 = soup.find_all(text="Jeti DS 16 2")
    # Jeti_DC_16 = soup.find_all(text="Jeti DC 16")
    # Jeti_DC_16_v2 = soup.find_all(text="Jeti DC 16 2")

    # if(Jeti_DC_16, Jeti_DC_16_v2, Jeti_DS_16, Jeti_DS_16_v2):
    #     send_mail()

    # print('Die Nummer {0} {1} {2} {3} wurden gezogen'.format(
    #     Jeti_DC_16, Jeti_DC_16_v2, Jeti_DS_16, Jeti_DS_16_v2))

    print(type(search_for_class))
    print(len(search_for_class))

    first_result = search_for_class[0]
    # print(type(first_result.h3))
    # print(type(first_result.div.a.text))
    # print(type(first_result.a.text))
    # print(type(first_result.p.text))
    # print(type(first_result.name.text))
    # print(type(first_result.title))
    print(type(first_result))
    print(type(first_result.text))
    # print(soup.div)


# def send_mail():

#     server_ssl = smtplib.SMTP_SSL('smtp.gmail.com', 465)
#     server_ssl.ehlo()
#     # server.starttls()
#     # server.ehlo()

#     server_ssl.login('Secure@gmail.com', 'SecurePassword')

#     subject = 'Es gibt ein neuer Post im RC-Network auf deine gespeicherte Anfragen. Sieh in dir an{Link to Post}'
#     body = 'Sieh es dir an Link: https://www.rc-network.de/forums/biete-rc-elektronik-zubeh%C3%B6r.135/'

#     msg = f"Subject: {subject}\n\n{body}"
#     emails = ["Secure@gmx.de"]

#     server_ssl.sendmail(
#         'Secure@gmail.com',
#         emails,
#         msg
#     )
#     print('e-Mail wurde versendet!')

#     server_ssl.quit


while(True):
    checkForSearchItem()
    time.sleep(600)
    # time.sleep(86400)

【问题讨论】:

    标签: python web-scraping


    【解决方案1】:

    当你想打印文本时不需要type()。type() function 只是为了查看变量的类型(int,str,...)。没有 type() 代码对我来说很好,打印文本。这意味着,在打印语句上而不是这样:

    print(type(first_result.text))
    

    这样写:

    print(first_result.text)
    

    我希望这就是你的问题,我可以帮助你。 当您需要帖子的 URI 时,您必须在帖子 div 中获取 a Tag 并从中提取您的 URI,如下所示:

    def checkForSearchItem():
    
        response = get(URL)
    
        soup = BeautifulSoup(response.content, "lxml")
    
        posts = soup.find_all('div', class_='structItem-title')
    
        for post in posts:
            a_tag = post.find_all('a')[0] # The a-tag inside the div
            link = a_tag.get('href') # The href inside the a-tag
            url = f'https://www.rc-network.de{link}' # The full URI because the 'link' looks like /threads/sensoren-von-graupner.11835933/
            print(post.text)
            print(url)
    

    【讨论】:

    • 傻了,谢谢。您能帮帮我吗,如果有包含这些标题之一的帖子,我如何获得链接?
    猜你喜欢
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多