BeautifulSoup-tag ничего не возвращает, хотя в нем есть элементы - PullRequest
0 голосов
/ 30 мая 2020

Попытка очистить указанный ниже веб-сайт. После извлечения href из a-тегов он не возвращает ничего. Но в этих тегах есть элементы. Вот код:

from selenium import webdriver
import time
from bs4 import BeautifulSoup


driver = webdriver.Chrome(r"E:\chromedriver_win32\chromedriver.exe")
url= "https://www.adb.org/projects/tenders/sector/information-and-communication-technology-1066"
driver.get(url)

content = driver.page_source.encode('utf-8').strip()
soup = BeautifulSoup(content,"html.parser")
div_tags = soup.findAll("div",{"class":"item-title"})
for tags in div_tags:
    a_tag=tags.find('a')
    link=a_tag.get('href')
    print(link)

div_tags -вывод напечатан:

[<div class="item-title">
<a href="/node/606921"><div class="item-title">Corporate Innovation Expert</div></a></div>, <div class="item-title">Corporate Innovation Expert</div>, <div class="item-title">
<a href="/node/605571"><div class="item-title">Communications Specialist</div></a></div>, <div class="item-title">Communications Specialist</div>, <div class="item-title">
<a href="/node/603231"><div class="item-title">Venture Partner Expert</div></a></div>, <div class="item-title">Venture Partner Expert</div>, <div class="item-title">
<a href="/node/457636"><div class="item-title">Partnerships &amp; Communications Manager</div></a></div>, <div class="item-title">Partnerships &amp; Communications Manager</div>, <div class="item-title">
<a href="/node/545151"><div class="item-title">Operations Associate</div></a></div>, <div class="item-title">Operations Associate</div>, <div class="item-title">

1 Ответ

2 голосов

Вот код:

from requests import get
import time
from bs4 import BeautifulSoup

response = get('https://www.adb.org/projects/tenders/sector/information-and-communication-technology-1066') 
soup = BeautifulSoup(response.text, 'html.parser')

div_tags = soup.findAll("div",class_="item-title")
link={}
for tags in div_tags:
    a_tag=tags.find('a')
    try:
      link[a_tag.text]= "https://www.adb.org"+a_tag.get('href')
    except:
      continue
#print(link)# print all text and value as dict
for x, y in link.items():
  print(x, y) # x print text y print link

Если вы хотите использовать текст и ссылки в качестве словаря, используйте print(link). Если вам нужен текст, используйте print(x) вместо l oop. Если вам нужны ссылки, используйте print(y) in вместо l oop.

Run Online: https://repl.it/repls/ScaredCommonParallelcomputing

...