【发布时间】:2020-09-05 09:24:45
【问题描述】:
我正在从这个网站https://nypost.com/search/China+COVID-19/page/2/?orderby=relevance 抓取新文章 我使用 for-loop 来获取每篇新闻文章的内容,但我无法为每篇文章组合段落。我的目标是将每篇文章存储在一个字符串中,所有的字符串都应该存储在 myarticle 列表中。
当我 print(myarticle[0]) 时,它会给我所有的文章。我希望它应该给我一篇文章。
任何帮助将不胜感激!
for pagelink in pagelinks:
#get page text
page = requests.get(pagelink)
#parse with BeautifulSoup
soup = bs(page.text, 'lxml')
containerr = soup.find("div", class_=['entry-content', 'entry-content-read-more'])
articletext = containerr.find_all('p')
for paragraph in articletext:
#get the text only
text = paragraph.get_text()
paragraphtext.append(text)
#combine all paragraphs into an article
thearticle.append(paragraphtext)
# join paragraphs to re-create the article
myarticle = [''.join(article) for article in thearticle]
print(myarticle[0])
为清楚起见,下面附上完整的代码
def scrape(url):
user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko'}
request = 0
urls = [f"{url}{x}" for x in range(1,2)]
params = {
"orderby": "relevance",
}
pagelinks = []
title = []
thearticle = []
paragraphtext = []
for page in urls:
response = requests.get(url=page,
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)
#throw a warning for non-200 status codes
if response.status_code != 200:
warn('Request: {}; Status code: {}'.format(request, response.status_code))
#Break the loop if the number of requests is greater than expected
if request > 72:
warn('Number of request was greater than expected.')
break
#parse the content
soup_page = bs(response.text, 'lxml')
#select all the articles for a single page
containers = soup_page.findAll("li", {'class': 'article'})
#scrape the links of the articles
for i in containers:
url = i.find('a')
pagelinks.append(url.get('href'))
#scrape the titles of the articles
for i in containers:
atitle = i.find(class_ = 'entry-heading').find('a')
thetitle = atitle.get_text()
title.append(thetitle)
for pagelink in pagelinks:
#get page text
page = requests.get(pagelink)
#parse with BeautifulSoup
soup = bs(page.text, 'lxml')
containerr = soup.find("div", class_=['entry-content', 'entry-content-read-more'])
articletext = containerr.find_all('p')
for paragraph in articletext:
#get the text only
text = paragraph.get_text()
paragraphtext.append(text)
#combine all paragraphs into an article
thearticle.append(paragraphtext)
# join paragraphs to re-create the article
myarticle = [''.join(article) for article in thearticle]
print(myarticle[0])
print(scrape('https://nypost.com/search/China+COVID-19/page/'))
【问题讨论】:
-
你能把剩下的代码加进去,我们可以检查.. url等
-
当然,我更新了描述。请检查:)
-
什么是 clear_output 和 warn.. 它们来自哪里?
-
我收到一条错误消息“超出最大重试次数”; clear_output 和警告。是为了解决这个问题。稍后我会抓取更多页面,clear_output 和 warn 可能会有所帮助。但是 clear_output 和 warn 与这个问题无关,你可以忽略它们:)
标签: python web-scraping beautifulsoup request web-crawler