关键词用python搜索网站上的文章

问题描述 投票:2回答:1

我刚加入你,自从我开始学习python以来已经有一个月了。我想用Python(http://aeconf.com/may2013.htm)从这个网站搜索关键字。

通常我手动点击视图摘要并在“关键词:”之后搜索单词。如何自动使用python执行此操作?我很抱歉我的英语不好:(

python article
1个回答
1
投票

你应该看看Selenium

pip install selenium

我提供了一个示例代码,说明它可以做什么,你应该测试一下。

示例代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException


caps = DesiredCapabilities().CHROME
caps["pageLoadStrategy"] = "normal"  
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'C:\Users\My-PC-Name\AppData\Local\Programs\Python\Python37-32\Scripts\chromedriver.exe')


url = "http://aeconf.com/may2000.htm" #Link
driver.get(url)
links = [x.get_attribute('href') for x in driver.find_elements_by_link_text('View Abstract')]
htmls = []

for link in links:
   driver.get(link)
   Keyword = [y.text for y in driver.find_elements_by_xpath("//font[2]/span[@style = 'mso-bidi-font-size: 1.0pt']")]
   if not Keyword: #If link is a dead link
     continue
   print(Keyword[0])
   htmls.append(driver.page_source)

在这个例子中,我将url更改为http://aeconf.com/may2000.htm我提供的代码主要得到你需要的“关键词”,但在某些情况下,“关键词”的索引位置会根据所述Url中的链接而改变。

“已更改”链接的输出:

Fiscal decentralization; Corruption; Tax evasion.
Incentive mechanism design; Walrasian allocations; Implementation.
Debt and equity flows; Asymmetric information; Bankruptcy cost; Market failures; 
Corrective taxation.
Transitory volatility; Price formation; Exogenous liquidity demand.
Investment horizon; Beta; Size; Book-to-market equity; CAPM.
G11, G13.                         #At This part you can see that the 'Key Words' printed are not correct
Portfolio constraints; Stochastic income; Relaxation-projection methods.
Foreign aid; Foreign borrowing; Capital accumulation.
Entrepreneurial ability; Asymmetric information; Liquidity constraints.
Contract; Human capital; Labor.
Endogenous structure of the division of labor; Dual economy; Endogenous trade policy regime.

如果我们将示例代码中的'url'变量更改为原始link,则更多情况下,即使第一个链接是死链接,索引位置也会更改。作为一个挑战,我会让你自己弄清楚:-)还有更多的模块可以像Selenium一样做同样的事情。希望这能让您对浏览器自动化,Web Scraping等更多感兴趣(Web Crawler等)。

只是一个提示(可能不是提示)您只需要更改“关键字”变量的索引位置以获得所需的“关键字”。

© www.soinside.com 2019 - 2024. All rights reserved.