【问题标题】:Moving to next page while scraping抓取时移动到下一页
【发布时间】:2019-07-13 16:11:18
【问题描述】:

在网页抓取和更改日期格式时移动到下一页

url_list 是一个 url 列表,其中一个是 http://www.moneycontrol.com/company-article/cadilahealthcare/news/CHC#CHC 我发现要移动到不同的年份和不同的页面,有一个 href 代码,但我似乎无法使用它。这是从第 1 页提取链接的代码。我想在所有可用的年份和可用页面中都这样做。

当我从 html 中提取日期时,它的格式为 [最后更新时间:IST 2019 年 2 月 7 日下午 3:05 |资料来源:Moneycontrol.com] 我想要 mm/dd/yy 格式的日期,我该怎么做呢?

for urls in url_list:
    html = requests.get(urls)
    soup = BeautifulSoup(html.text,'html.parser') # Create a BeautifulSoup object 
     
       # Retrieve a list of all the links and the titles for the respective links
       #word1,word2,word3 = "US","USA","USFDA"
     
    sub_links = soup.find_all('a', class_='arial11_summ')
    for links in sub_links:
        sp = BeautifulSoup(str(links),'html.parser')  # first convert into a string
        tag = sp.a
          #if word1 in tag['title'] or word2 in tag['title'] or word3 in tag['title']:
        category_links = Base_url + tag["href"]
        List_of_links.append(category_links)
        time.sleep(3)

我想要做的是抓取第一页然后移动到下一页等等,在抓取特定年份的可用页面之后,代码移动到下一年。请解释我将如何去做。

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    移至下一页:

    提取日期:子字符串仅获取日期时间,然后像这样解析时间和时区

    我使用 pytz 更新了设置时区

    input = 'Feb 07, 2019 03:05 PM IST'
    str_time = input[:len(input) - 4]
    str_timezone = input[len(input) - 3:]
    
    datetime_object = datetime.strptime(str_time, '%b %d, %Y %I:%M %p')
    if str_timezone == 'IST':
        # base on https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
        # assume it's Indian/Mauritius
        tz = pytz.timezone('Indian/Mauritius')
    else:
        tz = pytz.timezone('UTC')
    
    output = tz.localize(datetime_object)
    # test
    print(output.strftime('%X %x %z'))
    

    【讨论】:

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