【问题标题】:Unable to crawl multiples URLs无法抓取多个网址
【发布时间】:2020-04-09 15:14:56
【问题描述】:

我有一个函数,它可以两次抓取网页并查找特定类并在其中找到 href 标记。

url="https://www.poynter.org/ifcn-covid-19-misinformation/page/220/"

def url_parse(site):
   hdr = {'User-Agent': 'Mozilla/5.0'}
   req = Request(site,headers=hdr)
   page = urlopen(req)
   soup = BeautifulSoup(page)
   return soup

def article_link(URL):
   try:
      soup=url_parse(URL)
      for i in soup.find_all("a", class_="button entry-content__button entry-content__button--smaller"):
        link=i['href']
   except:
      pass    
return link



data['article_source']=""
for i, rows in data.iterrows():
   rows['article_source']= article_link(rows['url'])

问题

函数 url_parse 和 article_link 工作正常,但是当我使用函数 article_link 更新数据报内的单元格时,它在 1500 或 1000 个 URL 后停止工作。我知道我的笔记本电脑可能有一个 IP 地址,但我不知道如何解决它,因为没有错误消息。

期待

函数article_link解析数据框内的所有URL。

【问题讨论】:

  • 尝试将 pass 参数换成 print(err) 参数。要获得错误,请执行 except Exception 作为 err: 而不是 except: only。完成此操作后,报告您的发现,我们可以为您提供更好的帮助。
  • 我已经向主机发送了 10k 个请求,没有任何问题!
  • @αԋɱҽԃαмєяιcαη 有时,这不适合我,但有时会停止工作

标签: python dataframe beautifulsoup web-crawler


【解决方案1】:
import requests
from bs4 import BeautifulSoup
from concurrent.futures.thread import ThreadPoolExecutor

url = "https://www.poynter.org/ifcn-covid-19-misinformation/page/{}/"

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
}


def main(url, num):
    with requests.Session() as req:
        print(f"Extracting Page# {num}")
        r = req.get(url.format(num), headers=headers)
        soup = BeautifulSoup(r.content, 'html.parser')
        links = [item.get("href") for item in soup.findAll(
            "a", class_="button entry-content__button entry-content__button--smaller")]
        return links


with ThreadPoolExecutor(max_workers=50) as executor:
    futures = [executor.submit(main, url, num) for num in range(1, 238)]

for future in futures:
    print(future.result())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2022-06-25
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多