移动到下一页时抓取

2024-04-19 09:39:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

另外,当我从html中提取日期时,它的格式是 [上次更新时间:2019年2月7日下午03:05 IST |来源: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)

我想做的是先删除第一页,然后移到下一页,以此类推,在删除某一年的可用页之后,代码将移到下一年。请解释一下我该怎么做。你知道吗


Tags: the代码incomurlfortitlehtml
1条回答
网友
1楼 · 发布于 2024-04-19 09:39:41

移到下一页:

提取date:sub字符串以仅获取datetime,然后像这样解析时间和时区

我使用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'))

相关问题 更多 >