【发布时间】:2021-10-30 11:55:11
【问题描述】:
我需要从网站中提取信息。该网站在以下路径中有信息:
<div class="accordion-block__question">
<div class="accordion-block__text">Server</div></div>
...
<div class="block__col"><b>Country</b></div>
跑步
try:
# Country
c=driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'Country')]").get_attribute('textContent')
country.append(c)
except:
country.append("Error")
我创建了一个包含所有错误的 df。我对所有领域都感兴趣(但要解决这个问题,只有一个会很棒),包括 Trustscore(数字),但我不知道是否有可能得到它。我在 Chrome 上使用 selenium,网络驱动程序。 网址是https://www.scamadviser.com/check-website。
代码
这是完整的代码:
def scam(df):
chrome_options = webdriver.ChromeOptions()
trust=[]
country = []
isp_country = []
query=df['URL'].unique().tolist()
driver=webdriver.Chrome('mypath',chrome_options=chrome_options))
for x in query:
wait = WebDriverWait(driver, 10)
response=driver.get('https://www.scamadviser.com/check-website/'+x)
try:
wait = WebDriverWait(driver, 30)
# missing trustscore
# Country
c=driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'Country')]")).get_attribute('innerText')
country.append(c)
# ISP country
ic=driver.find_element_by_xpath("//div[contains(@class,'block__col') and contains(text(),'ISP')]").get_attribute('innerText')
isp_country.append(ic)
except:
# missing trustscore
country.append("Error")
isp_country.append("Error")
# Create dataframe
dict = {'URL': query, 'Trustscore':trust, 'Country': country, 'ISP': isp_country}
df=pd.DataFrame(dict)
driver.quit()
return df
您可以尝试例如 df['URL'] 等于
stackoverflow.com
gitHub.com
【问题讨论】:
标签: python selenium selenium-webdriver web-scraping webdriver