蓝色元素的 href 值是我想从此 HTML 访问的内容
我尝试了几种方法来打印链接,但没有成功。
我的代码如下:-
discover_page = BeautifulSoup(r.text, 'html.parser')
finding_accounts = discover_page.find_all("a", class_="author track")
print(len(finding_accounts))
finding_accounts = discover_page.find_all('a[class="author track"]')
print(len(finding_accounts))
accounts = discover_page.select('a', {'class': 'author track'})['href']
print(len(accounts))
Output:-
0
0
TypeError: 'dict' object is not callable
网页的网址是https://society6.com/discover但网址更改为https://society6.com/society?show=2登录我的帐户后
我在这里做错了什么?
注意:-我在这里使用 selenium chrome 浏览器。这里给出的答案在我的终端中有效,但在我运行文件时无效
我的完整代码:-
from selenium import webdriver
import time
import requests
from bs4 import BeautifulSoup
import lxml
driver = webdriver.Chrome()
driver.get("https://society6.com/login?done=/")
username = driver.find_element_by_id('email')
username.send_keys("[email protected]")
password = driver.find_element_by_id('password')
password.send_keys("sultan1997")
driver.find_element_by_name('login').click()
time.sleep(5)
driver.find_element_by_link_text('My Society').click()
driver.find_element_by_link_text('Discover').click()
time.sleep(5)
r = requests.get(driver.current_url)
r.raise_for_status()
'''discover_page = BeautifulSoup(r.html.raw_html, 'html.parser')
finding_accounts = discover_page.find_all("a", class_="author track")
print(len(finding_accounts))
finding_accounts = discover_page.find_all('a[class="author track"]')
print(len(finding_accounts))
links = []
for a in discover_page.find_all('a', class_ = 'author track'):
links.append(a['href'])
#links.append(a.get('href'))
print(links)'''
#discover_page.find_all('a')
links = []
for a in discover_page.find_all("a", attrs = {"class": "author track"}):
links.append(a['href'])
#links.append(a.get('href'))
print(links)
#soup.find_all("a", attrs = {"class": "author track"})'''
soup = BeautifulSoup(r.content, "lxml")
a_tags = soup.find_all("a", attrs={"class": "author track"})
for a in soup.find_all('a',{'class':'author track'}):
print('https://society6.com'+a['href'])
文档中的代码是我正在尝试使用的代码