【发布时间】: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