【问题标题】:BeautifulSoup: 6k records - but stops after parsing 20 linesBeautifulSoup:6k 条记录 - 但在解析 20 行后停止
【发布时间】:2021-07-30 22:12:28
【问题描述】:

为了快速概述欧洲的一系列免费志愿服务机会

旨在获取所有 6k 目标页面:https://europa.eu/youth/volunteering/organisation/48592 见下文 - 目标目标和所需数据的图像、解释和描述。

我们获取..

https://europa.eu/youth/volunteering/organisation/50162
https://europa.eu/youth/volunteering/organisation/50163

and so forth and so forth 

由于我们有超过 6000 条记录 - 我承认我得到了结果。但脚本只返回 20 条记录 - 即 20 行。

查看我目前的方法:我在这里运行这个迷你方法:

import requests
from bs4 import BeautifulSoup
import re
import csv
from tqdm import tqdm

first = "https://europa.eu/youth/volunteering/organisations_en?page={}"
second = "https://europa.eu/youth/volunteering/organisation/{}_en"

def catch(url):
    with requests.Session() as req:
        pages = []
        print("Loading All IDS\n")
        for item in tqdm(range(0, 347)):
            r = req.get(url.format(item))
            soup = BeautifulSoup(r.content, 'html.parser')
            numbers = [item.get("href").split("/")[-1].split("_")[0] for item in soup.findAll(
                "a", href=re.compile("^/youth/volunteering/organisation/"), class_="btn btn-default")]
            pages.append(numbers)
        return numbers

def parse(url):
    links = catch(first)
    with requests.Session() as req:
        with open("Data.csv", 'w', newline="", encoding="UTF-8") as f:
            writer = csv.writer(f)
            writer.writerow(["Name", "Address", "Site", "Phone",
                             "Description", "Scope", "Rec", "Send", "PIC", "OID", "Topic"])
            print("\nParsing Now... \n")
            for link in tqdm(links):
                r = req.get(url.format(link))
                soup = BeautifulSoup(r.content, 'html.parser')
                task = soup.find("section", class_="col-sm-12").contents
                name = task[1].text
                add = task[3].find(
                    "i", class_="fa fa-location-arrow fa-lg").parent.text.strip()
                try:
                    site = task[3].find("a", class_="link-default").get("href")
                except:
                    site = "N/A"
                try:
                    phone = task[3].find(
                        "i", class_="fa fa-phone").next_element.strip()
                except:
                    phone = "N/A"
                desc = task[3].find(
                    "h3", class_="eyp-project-heading underline").find_next("p").text
                scope = task[3].findAll("span", class_="pull-right")[1].text
                rec = task[3].select("tbody td")[1].text
                send = task[3].select("tbody td")[-1].text
                pic = task[3].select(
                    "span.vertical-space")[0].text.split(" ")[1]
                oid = task[3].select(
                    "span.vertical-space")[-1].text.split(" ")[1]
                topic = [item.next_element.strip() for item in task[3].select(
                    "i.fa.fa-check.fa-lg")]
                writer.writerow([name, add, site, phone, desc,
                                 scope, rec, send, pic, oid, "".join(topic)])


parse(second)

但这在解析 20 个结果后停止

注意:我想返回页面而不是数字。

因为我想遍历页面而不是数字;但无论如何 - 如果我从返回编号更改为返回页面 - 我不会得到更好的结果。

这里我似乎有一些错误:我猜catch函数有一个花哨的错误:这里我们返回数字,但我很确定这是一个错误:我们打算返回页面,这意味着当我们在另一个函数中迭代 catch(first) 的结果时,我们并没有迭代所有想要的东西。 我想我需要包含一个修复:我们需要在该函数的底部返回页面,而不是返回数字

也就是说:因为我想遍历页面而不是数字;但无论如何 - 如果我从返回编号更改为返回页面 - 我不会得到更好的结果。

任何想法 - 如何让解析器给出所有 6k 结果。

【问题讨论】:

  • 您好,您忽略了添加明确的问题。
  • 嗨 - 我有问题:脚本只能返回 20 条记录。但是那里有更多的 tan 6 k 记录。所以我在代码中有一个错误。在它说:返回数字 - 我想我必须重写这个
  • 我已经添加了更多的词来解释我猜错的地方 - 亲爱的 QHarr - 我期待收到你的来信

标签: python csv beautifulsoup request


【解决方案1】:

您可以使用此示例来解析页面:

import pandas
import requests
import pandas as pd
from bs4 import BeautifulSoup


def safe_get(to_find, what_next, if_not_found="N/A"):
    if to_find:
        return what_next(to_find)
    return if_not_found


first_url = "https://europa.eu/youth/volunteering/organisations_en?page={}"

links = []
for page in range(0, 3):  # <--- increase number of pages here
    u = first_url.format(page)
    soup = BeautifulSoup(requests.get(u).content, "html.parser")

    for a in soup.select("h5 > a"):
        links.append("https://europa.eu" + a["href"])

data = []
for l in links:
    print(l)
    soup = BeautifulSoup(requests.get(l).content, "html.parser")

    name = safe_get(soup.select_one("h5"), lambda t: t.text)
    address = safe_get(
        soup.select_one(".fa-location-arrow"),
        lambda t: t.parent.get_text(strip=True),
    )
    link = safe_get(
        soup.select_one(".fa-external-link"), lambda t: t.find_next("a")["href"]
    )
    phone = safe_get(
        soup.select_one(".fa-phone"), lambda t: t.find_next(text=True).strip()
    )
    desc = safe_get(
        soup.select_one("h3 ~ p"),
        lambda t: t.get_text(strip=True, separator="\n"),
    )
    scope = safe_get(
        soup.select_one(".fa-asterisk"),
        lambda t: t.find_next("span").get_text(strip=True),
    )
    receiving = safe_get(
        soup.select_one('td:-soup-contains("Receiving") ~ td'),
        lambda t: t.get_text(strip=True),
    )
    sending = safe_get(
        soup.select_one('td:-soup-contains("Sending") ~ td'),
        lambda t: t.get_text(strip=True),
    )
    pic = safe_get(
        soup.find("span", text=lambda t: t and t.startswith("PIC")),
        lambda t: t.text.split()[-1],
    )
    oid = safe_get(
        soup.find("span", text=lambda t: t and t.startswith("OID")),
        lambda t: t.text.split()[-1],
    )
    topics = ", ".join(
        [t.find_next(text=True).strip() for t in soup.select("p > .fa-check")]
    )

    data.append(
        (
            name,
            address,
            link,
            phone,
            desc,
            scope,
            receiving,
            sending,
            pic,
            oid,
            topics,
        )
    )

df = pd.DataFrame(
    data,
    columns=[
        "Name",
        "Address",
        "Site",
        "Phone",
        "Description",
        "Scope",
        "Rec",
        "Send",
        "PIC",
        "OID",
        "Topic",
    ],
)
print(df)
df.to_csv("data.csv", index=False)

创建data.csv(来自 Libre Office 的屏幕截图):

【讨论】:

  • what_next 是用户定义的函数吗?
  • 亲爱的 Andrej - 哇 - 太好了:非常感谢您提供的出色方法。它看起来棒极了。目前我回来了:`NotImplementedError: ':-soup-contains' 伪类目前没有实现 - 注意:我用 Python 3.7.6 运行 conda-base Python 3.7.6` - 我猜我必须更新这个。我会在当天晚些时候这样做。非常感谢您的出色方法。看起来棒极了。
  • @zero :-soup-contains 是bs4 的最新版本。如果您使用旧版本,可以将其更改为:contains。
  • @QHarr 是的,what_next 是保存函数的参数。在处理不存在的标签时,我正在考虑使用 := 运算符,但 OP 使用的是 Python3.7。所以我创建了这个safe_get 函数。不过普通的if-else也可以。
  • 你好 Andrej - 再次:非常感谢:我会在这里照顾我的系统的当前更新。我将在当天晚些时候执行此操作,并在晚上晚些时候测试代码。通过快速测试 - 我还收到了以下投诉:` Traceback(最近一次通话最后一次):文件“/home/martin/dev/europe_volun_with_pandas_old_bs4.py”,第 91 行,在 df = pd.DataFrame(NameError : 名称“pd”未定义`
猜你喜欢
  • 2018-11-15
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
相关资源
最近更新 更多