मैं एंकर टेक्स्ट Personal Website
के साथ सभी URL प्राप्त करने का प्रयास कर रहा हूं। मैं जिस HTML के बारे में बात कर रहा हूं उसका एक उदाहरण यहां दिया गया है।
<a href="http://example.com" class>Personal website</a>
अभी, मैं इसके साथ ऐसा करने की कोशिश कर रहा हूं:
for link in bio_link_list:
site = soup.find_all("a", href = True, text = "Personal Website")
site_list.append(site)
जहां bio_link_list
केवल लिंक की एक सूची है जिसे मैं स्क्रैप कर रहा हूं। लेकिन यह सिर्फ एक खाली सूची देता है। स्पष्टीकरण के लिए, मुझे उन यूआरएल की एक सूची चाहिए जिसमें वह विशिष्ट एंकर टेक्स्ट हो।
4 जवाब
आपके कोड के काम नहीं करने का कारण यह है कि कोई भी एंकर class Perosnal website
का नहीं है। लेकिन स्रोत कोड का निरीक्षण करने के बाद, आप आसानी से सभी a
तत्वों को पकड़ सकते हैं और उन्हें bio
स्ट्रिंग के लिए फ़िल्टर कर सकते हैं।
ये कोशिश करें:
import requests
from bs4 import BeautifulSoup
url = "https://www.stern.nyu.edu/faculty/search_name_form"
soup = BeautifulSoup(requests.get(url).content, "html.parser").find_all("a")
bio_links = [a['href'] for a in soup if "bio" in a['href']]
print(f"Found {len(bio_links)} bio links:")
print(bio_links)
आउटपुट:
Found 465 bio links
['https://www.stern.nyu.edu/faculty/bio/viral-acharya', 'https://www.stern.nyu.edu/faculty/bio/allen-adamson', 'https://www.stern.nyu.edu/faculty/bio/beril-afsar',...]
इससे 465
परिणाम मिलते हैं, जो बिल्कुल वैसा ही है जैसा कि <div class="results">465 results</div>
के अंतर्गत पृष्ठ पर होता है।
संपादित करें: मैंने शुरू में आपके प्रश्न को गलत समझा, लेकिन यहां जैव यूआरएल से व्यक्तिगत वेब साइट प्राप्त करने का एक तरीका है।
import requests
from bs4 import BeautifulSoup
url = "https://www.stern.nyu.edu/faculty/search_name_form"
soup = BeautifulSoup(requests.get(url).content, "html.parser").find_all("a")
bio_links = [a['href'] for a in soup if "bio" in a['href']]
personal_sites = []
for link in bio_links:
print(link)
soup = BeautifulSoup(requests.get(link).content, "html.parser")
personal_sites.extend(
[
a["href"] for a in soup.find_all("a")
if a.getText() == "Personal website"
]
)
print(personal_sites)
आउटपुट:
['http://pages.stern.nyu.edu/~sternfin/vacharya/public_html/~vacharya.htm', 'http://brandsimple.com/about-allen-adamson/', 'http://pages.stern.nyu.edu/~sternfin/talbanese/', 'http://www.stern.nyu.edu/~ealtman', ...]
अंत में, आप व्यक्तिगत वेबसाइट लिंक प्राप्त करने के लिए lxml
मॉड्यूल और Xpath का उपयोग करके चीजों को गति दे सकते हैं।
import requests
from bs4 import BeautifulSoup
from lxml import html
url = "https://www.stern.nyu.edu/faculty/search_name_form"
ps_xpath = '//*[@id="bio-details"]/div[2]/p[3]/a[2]/@href'
def get_page(url: str):
return requests.get(url).content
def get_personal_site(url: str):
ps = html.fromstring(get_page(url)).xpath(ps_xpath)
return next(iter(ps), None)
def scrape_sites(bio_links: list):
return [get_personal_site(link) for link in bio_links]
soup = BeautifulSoup(get_page(url), "html.parser").find_all("a")
links = [a['href'] for a in soup if "bio" in a['href']]
print(scrape_sites(links))
मुझे लगता है कि एक साधारण अगर शर्त चाल चलेगी
for url in all_bio_links:
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
links=[]
for a in soup.find_all('a', href=True):
if a.text=="Personal website":
links.append(a['href'])
ऐसा लगता है कि आप link
चर को नज़रअंदाज़ कर रहे हैं। क्या आप सुनिश्चित हैं कि आप पहले link
के लिए अनुरोध नहीं करना चाहते हैं और फिर परिणामी HTML को स्क्रैप करना चाहते हैं? किसी भी तरह, यह कोशिश करो।
urls = [tag["href"] for tag in soup.find_all("a") if tag.getText() == "Personal Website" and tag["href"]]
यह उत्तर मानता है कि आपके पास bio_link_list में प्रोफ़ाइल URL हैं। यदि नहीं, तो प्रोफ़ाइल पृष्ठों का आकलन करने से पहले नीचे दिए गए कोड के परिवर्तन का उपयोग करके जानकारी को परिमार्जन करने का एक तरीका है।
आप इस उपाय को आजमा सकते हैं। टेक्स्ट फ़ाइल List.txt यूआरएल का स्रोत है। प्रति पंक्ति एक। यह बाद में उपयोग के लिए सभी आउटपुट को एक csv फ़ाइल में भी लिखेगा।
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
from datetime import datetime
from lxml.html import parse
import re
dataList = []
New_date_id = datetime.now().strftime("%Y%m%d-%H%M%S")
with open('List.txt') as urlList:
urls = (line for line in urlList)
for url in urls:
site = urlopen(url)
soup = BeautifulSoup(site, "lxml")
for item in soup.findAll('a', text = re.compile('Personal website')):
newItem = (item.get ('href'))
dataList.append(newItem)
#with open(date_id + 'data.txt', "w", encoding='utf-8') as fhandle:
with open(New_date_id + "_"+ 'data.csv', 'w', newline='', encoding='utf-8') as csv_file:
for line in dataList:
csv_file.write(f'{line} \n')
संबंधित सवाल
नए सवाल
python-3.x
पायथन प्रोग्रामिंग के बारे में प्रश्नों के लिए जो भाषा के संस्करण 3+ के लिए विशिष्ट हैं। सभी पायथन सवालों पर अधिक जेनेरिक [अजगर] टैग का उपयोग करें, और केवल यह जोड़ें यदि आपका प्रश्न संस्करण-विशिष्ट है। पायथन 2 प्रश्नों के लिए [अजगर -2] टैग का उपयोग करें।