我一直在使用Python4和BeautifulSoup 4从非全球网站中抓取数据。那里有一些公司,比如这一家:https://www.unglobalcompact.org/what-is-gc/participants/2968-Orsted-A-S有推特账户。我想访问推特账号的名字。问题是它在没有src属性的iframe中。我知道iframe是由一个不同于网站其余部分的请求调用的,但我想知道现在是否可以在看不到src属性的情况下访问它?
发布于 2020-10-19 18:27:57
您可以使用selenium来完成此操作。下面是完整的代码:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
url = "https://www.unglobalcompact.org/what-is-gc/participants/2968-Orsted-A-S "
driver = webdriver.Chrome()
driver.get(url)
iframe = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="twitter-widget-0"]')))
driver.switch_to.frame(iframe)
names = driver.find_elements_by_xpath('//*[@class="TweetAuthor-name Identity-name customisable-highlight"]')
names = [name.text for name in names]
try:
name = max(set(names), key=names.count) #Finds the most frequently occurring name. This is because the same author has also retweeted tweets made by others. These retweets would contain the name of other people. The most frequently occurring name is the name of the author.
print(name)
except ValueError:
print("No Twitter Feed Found!")
driver.close()输出:
Ørstedhttps://stackoverflow.com/questions/64422745
复制相似问题