【发布时间】:2020-09-10 16:38:30
【问题描述】:
我正在从动态网页中抓取内容。 https://www.nytimes.com/search?query=china+COVID-19我想获取所有新闻文章的内容(共26783篇)。我无法迭代页面,因为在此网站上您必须单击“显示更多”才能加载下一页。
因此,我使用的是 webdriver.ActionChians。该代码没有显示任何错误消息,但每隔几秒钟就会弹出一个新窗口,看起来每次都是同一页。这个过程似乎没完没了,我在 2 小时后中断了它。我使用了代码“print(article)”,但没有显示。 有人可以帮我解决这个问题吗?非常感谢您的帮助!
import time
import requests
from bs4 import BeautifulSoup
import json
import string
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
# Initialize webdriver.Chrome and webdriver.ActionChains only once
chromedriver_path = 'C:/chromedriver.exe'
driver = webdriver.Chrome(chromedriver_path)
action = webdriver.ActionChains(driver)
# Get to the page
driver.get('https://www.nytimes.com/search?query=china+COVID-19')
soup = BeautifulSoup(driver.page_source, 'html.parser')
# While button is present
while soup.find('button', {'data-testid': 'search-show-more-button'}) != None:
# Find button
button = driver.find_element_by_xpath('//button[@type="button"][contains(.,"Show More")]')
# Move to it to avoid false-clicking other elements
action.move_to_element(button).perform()
# Click the button
button.click()
# Redefine variable 'soup' in case if button dissapeared, so the 'while' loop will end
soup = BeautifulSoup(driver.page_source, 'html.parser')
search_results = soup.find('ol', {'data-testid':'search-results'})
links = search_results.find_all('a')
for link in links:
link_url = link['href']
response = requests.get(base + link_url)
soup_link = BeautifulSoup(response.text, 'html.parser')
scripts = soup_link.find_all('script')
for script in scripts:
if 'window.__preloadedData = ' in script.text:
jsonStr = script.text
jsonStr = jsonStr.split('window.__preloadedData = ')[-1]
jsonStr = jsonStr.rsplit(';',1)[0]
jsonData = json.loads(jsonStr)
article = []
for k, v in jsonData['initialState'].items():
w=1
try:
if v['__typename'] == 'TextInline':
article.append(v['text'])
#print (v['text'])
except:
continue
article = [ each.strip() for each in article ]
article = ''.join([('' if c in string.punctuation else ' ')+c for c in article]).strip()
print(article)
myarticle.append(article)
df = pd.DataFrame(myarticle, columns = ['article'])
df.to_csv('NYtimes.csv')
print("Complete")
browser.quit()
输出
---------------------------------------------------------------------------
ElementClickInterceptedException Traceback (most recent call last)
<ipython-input-7-1515a65b3c60> in <module>
24 try:
---> 25 button.click()
26 break
~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
79 """Clicks the element."""
---> 80 self._execute(Command.CLICK_ELEMENT)
81
~\anaconda3\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
632 params['id'] = self._id
--> 633 return self._parent.execute(command, params)
634
~\anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
320 if response:
--> 321 self.error_handler.check_response(response)
322 response['value'] = self._unwrap_value(
~\anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
ElementClickInterceptedException: Message: element click intercepted: Element <button data-testid="search-show-more-button" type="button">...</button> is not clickable at point (509, 656). Other element would receive the click: <div class="css-1n5jm1v">...</div>
(Session info: chrome=83.0.4103.61)
During handling of the above exception, another exception occurred:
NameError Traceback (most recent call last)
<ipython-input-7-1515a65b3c60> in <module>
25 button.click()
26 break
---> 27 except ElementClickInterceptedException:
28 time.sleep(0.5)
29 # Redefine variable 'soup' in case if button dissapeared, so the 'while' loop will end
NameError: name 'ElementClickInterceptedException' is not defined
【问题讨论】:
标签: python selenium selenium-webdriver web-scraping beautifulsoup