Я очищаю ссылки новостных статей с этой страницы: https://time.com/search/?q=China%20COVID-19&page=1 Я написал код для получения ссылок со страницы 1 и страницы 2, но он возвращает статьи только со страницы 1. Я не знаю, как решить эту проблему, чтобы она успешно возвращала результаты с нескольких страниц.
def scrap(url):
user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko'}
request = 0
params = {
'q': 'China%20COVID-19',
}
pagelinks = []
myarticle = []
for page_no in range(1,3):
params['page'] = page_no
response = requests.get(url=url,
headers=user_agent,
params=params)
# controlling the crawl-rate
start_time = time()
#pause the loop
sleep(randint(8,15))
#monitor the requests
request += 1
elapsed_time = time() - start_time
print('Request:{}; Frequency: {} request/s'.format(request, request/elapsed_time))
clear_output(wait = True)
#parse the content
soup_page = bs(response.text, 'lxml')
#select all the articles for a single page
containers = soup_page.findAll("article", {'class': 'partial tile media image-top margin-16-right search-result'})
scrape the links of the articles
for i in containers:
url = i.find('a')['href']
pagelinks.append(url)
print(pagelinks)
scrap('https://time.com/search/')
Мы будем очень благодарны за любые предложения!