【问题标题】:Python Selenium with BeautifulSoup for multiple links带有 BeautifulSoup 的 Python Selenium 用于多个链接
【发布时间】:2021-04-26 06:25:28
【问题描述】:

我想从多个网页中提取链接。提取时一切正常,但对于多个 url,第一个 url 得到两次,最后一个没有得到。这是什么原因?

import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import csv
from bs4 import BeautifulSoup

URLs = ["https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/1","https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/2",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/3","https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/4","https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/5",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/6","https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/7"]

driver = webdriver.Chrome(ChromeDriverManager().install())

file = open('linkler.csv', 'w+', newline='')
writer = csv.writer(file)
writer.writerow(['linkler'])


for link in URLs:
  driver.get(link)

  html_source = driver.page_source

  soup = BeautifulSoup(html_source, "html.parser")

  for links in soup.findAll('a', attrs={'href': re.compile("^/soccer/turkey/super-lig-2019-2020/")}):
    writer.writerow([links.get('href')])


driver.quit()

【问题讨论】:

  • “最后一个没有得到”是什么意思 - 请详细解释并改进您的问题。谢谢
  • 我的意思是url列表中的最后一个url。

标签: python selenium web-scraping beautifulsoup


【解决方案1】:

经过大量扫描后,我发现了问题,如果没有休息时间,该网站会阻止您的请求,所以我通过添加睡眠时间来解决它!现在你的代码可以正常工作了,我测试一下!

import re
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import csv
from bs4 import BeautifulSoup
import time

URLs = ["https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/1",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/2",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/3",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/4",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/5",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/6",
        "https://www.oddsportal1.com/soccer/turkey/super-lig-2019-2020/results/#/page/7"]

driver = webdriver.Chrome(ChromeDriverManager().install())

file = open('linkler.csv', 'w+', newline='')
writer = csv.writer(file)
writer.writerow(['linkler'])

for link in URLs:
    driver.get(link)
    time.sleep(5)
    html_source = driver.page_source

    soup = BeautifulSoup(html_source, "html.parser")

    for links in soup.findAll('a', attrs={'href': re.compile("^/soccer/turkey/super-lig-2019-2020/")}):
        writer.writerow([links.get('href')])

driver.quit()

【讨论】:

  • 感谢您的努力。这对我很有帮助。
【解决方案2】:

会发生什么?

获得一些重复是由网站上的重复和您匹配的 regex 引起的,因此脚本按设计工作 - 好消息您可以解决这个问题 ;)

如何避免重复写?

创建一个仅包含唯一hreflist,并检查新抓取的href 是否存在。如果不将其写入csv并更新list(也可以稍后将列表写入csv。)

示例

...
file = open('linkler.csv', 'w+', newline='')
writer = csv.writer(file)
writer.writerow(['linkler'])

hrefList = []

for link in URLs:
    driver.get(link)

    html_source = driver.page_source

    soup = BeautifulSoup(html_source, "html.parser")
    
    for links in soup.findAll('a', attrs={'href': re.compile("^/soccer/turkey/super-lig-2019-2020/")}):
        if links.get('href') not in hrefList:
            hrefList.append(links.get('href'))
            writer.writerow([links.get('href')])

file.close()
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-10
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多