【发布时间】:2022-01-09 15:06:17
【问题描述】:
我正在尝试使用 selenium 和 beautifulsoup 抓取特定网站。想法是在熊猫数据框中获取每个页面的链接及其对应的段落。
所以生成的数据框会是这样的
Link Paras
https://www.<website>.com contents of all <p> tags
/specific_page.html
为此,我使用以下代码 sn-ps:
driver = webdriver.Chrome(executable_path='D:\WebScrapp\chromedriver.exe')
driver.get(url)
elem = driver.find_elements_by_xpath("//a[@href]")
link=[]
para = []
for e in elem:
try:
page = requests.get(e.get_attribute('href'))
soup = bs(page.content,'lxml')
paras = soup.find_all('p')
for p in paras:
if '<seacrh_strng>' in p.text:
link.appned(str(e.get_attribute('href')))
para.append(p.text)
except:
print('InvalidSchema: No connection adapters')
df = pd.DataFrame(zip(link,para),columns=['Link','Para'])
有了以上内容,我面临以下问题:
- 大多数时候(或
elem中的大多数元素)它会被except阻塞,从而打印'InvalidSchema: No connection adapters'。 - 上述技术相当缓慢。
例如。我尝试过像https://www.cognizant.com 或https://www.sas.com 或https://www.bmc.com 这样的网址,但数据框中没有任何内容。很难相信这些网站没有使用<p>作为标签!!
事实上我已经尝试过paras = soup.find_all(re.compile('^h[1-6]$')),但没有运气!
我错过了什么?
【问题讨论】:
标签: python selenium beautifulsoup