【问题标题】:Scraping URLs using BeautifulSoup使用 BeautifulSoup 抓取 URL
【发布时间】:2016-04-01 11:27:10
【问题描述】:

我正在搜索板球赛程的网站。我正在使用美味的汤。 这是网址

www.ecb.c0.uk/stats/fixtures-results?m=1&y=2016

这是指 2016 年 1 月的所有赛程。
我也在尝试刮其他年份和月份。作为抓取的一部分,有没有办法可以更改代码中的年份?我必须在 URL 中放置一个变量吗?我是否必须通过列表形成循环或循环?

 from bs4 import BeautifulSoup
 import requests

 html = requests.get("http://www.ecb.co.uk/stats/fixtures-results?m=1& y=2016").text
 soup = BeautifulSoup(html,'lxml')

【问题讨论】:

    标签: python html beautifulsoup


    【解决方案1】:

    您可以使用两个循环,第一个循环数年,第二个循环数月:

    base_url = "http://www.ecb.co.uk/stats/fixtures-results?m={month}&y={year}"
    
    for year in range(2000, 2017):
        for month in range(1, 13):
            requests.get(base_url.format(month=month, year=year))
    

    或更短的变体itertools:

    for year, month in itertools.product(range(2000, 2017), range(1, 13)):
        requests.get(base_url.format(month=month, year=year))
    

    【讨论】:

      猜你喜欢
      • 2021-09-01
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多