【问题标题】:how to scrape the data from the url in python using beautifulsoup如何使用beautifulsoup从python中的url中抓取数据
【发布时间】:2019-12-03 12:39:04
【问题描述】:

我是网络抓取的新手,实际上我需要的是使用以下 URL:http://desiopt.com/search-results-jobs/ 有 37116 个工作,我想抓取所有公司信息(37116 个工作公司信息)。这是在上面提到的 url 中的 View job details 中。任何人请帮我解决这个任务。

【问题讨论】:

  • 向我们展示您的代码以及您的尝试!还包括您试图在那里解析哪些信息?
  • 尝试发出请求/呼叫 37,000 多次是一项艰巨的任务。此外,您可能需要阅读网站的条款和条件,因为它们似乎禁止“......使用或尝试使用引擎、手动或自动化软件、工具、设备、代理、脚本机器人或其他方式、设备、机制或进程(包括但不限于浏览器、蜘蛛、机器人、化身或智能代理)导航、搜索、访问、“抓取”、“抓取”或“蜘蛛”任何网页或网站上提供的任何服务比可用的搜索引擎和搜索代理..."
  • @αԋɱҽԃαмєяιcαη,感谢您的关注,我得到了您共享的 CSV 格式的数据。我需要代码以及它是如何工作的,并且我想学习,因为我是网络抓取的新手。谢谢
  • @chitown88 在下面查看我的答案:P
  • @chitown88 是的,他也只需要concurrent.futures 来加快进程:D

标签: python-3.x selenium web-scraping beautifulsoup


【解决方案1】:

使用以下url

https://desiopt.com/search-results-jobs/?action=search&page=&listings_per_page=&view=list

这是您将编辑的两个参数page=listings_per_page=

目前该网站确实有37091 职位。

经过我的测试,我确实看到listings_per_page 每页受到1000 的限制。

示例:https://desiopt.com/search-results-jobs/?action=search&page=1&listings_per_page=1000&view=list

所以你需要从page=1循环到page=38并设置listings_per_page=1000

这意味着1000 每页结果 * 38 page = 38000

之后

您将收集所有链接并将其传递给list,条件是删除重复项,以防您担心sort。否则,只需将其传递给不接受重复的set,但它不关心sort。然后你可以解析listset中的每一个url来收集信息。

顺便说一句,我将遍历371页面,每个页面包含100个项目,所以我会得到37100url(如果最后一页的url少于100,则更少) 并从中删除重复项,然后解析:

import requests
from bs4 import BeautifulSoup
import csv

links = []
try:
    for item in range(1, 372):
        print(f"Extraction Page# {item}")
        r = requests.get(
            f"https://desiopt.com/search-results-jobs/?action=search&page={item}&listings_per_page=100&view=list")
        if r.status_code == 200:
            soup = BeautifulSoup(r.text, 'html.parser')
            for item in soup.findAll('span', attrs={'class': 'captions-field'}):
                for a in item.findAll('a'):
                    a = a.get('href')
                    if a not in links:
                        links.append(a)
except KeyboardInterrupt:
    print("Good Bye!")
    exit()

data = []
try:
    for link in links:
        r = requests.get(link)
        if r.status_code == 200:
            soup = BeautifulSoup(r.text, 'html.parser')
            for item in soup.findAll('div', attrs={'class': 'compProfileInfo'}):
                a = [a.text.strip() for a in item.findAll('span')]
                if a[6] == '':
                    a[6] = 'N/A'
                data.append(a[0:7:2])
except KeyboardInterrupt:
    print("Good Bye!")
    exit()

while True:
    try:
        with open('output.csv', 'w+', newline='') as file:
            writer = csv.writer(file)
            writer.writerow(['Name', 'Phone', 'Email', 'Website'])
            writer.writerows(data)
            print("Operation Completed")
    except PermissionError:
        print("Please Close The File")
        continue
    except KeyboardInterrupt:
        print("Good Bye")
        exit()
    break

结果可以在这里查看: Click Here

输出是 1885 Rows,因为我已经让脚本在解析之前为公司删除重复的 links

在线运行代码:Click Here

【讨论】:

  • @ahmed American,谢谢。
【解决方案2】:

您可以通过将 page=1 从 1 替换为 371:http://desiopt.com/search-results-jobs/?searchId=1575377155.0222&action=search&page=1&listings_per_page=100&view=list 来循环访问此 URL,并使用 xpath //*[@class="viewDetails"]/a/@href 获取查看详细信息的所有标签,然后从各个页面获取所需的所有详细信息。

【讨论】:

    猜你喜欢
    • 2022-10-12
    • 1970-01-01
    • 2021-12-17
    • 2019-05-23
    • 2018-04-15
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多