我如何使用网络爬虫技术提取类=main-bullet的链接?

问题描述 投票:0回答:2

我使用了这个,使用这个我将提取所有的网页,但我想特定的链接,这是主要bullet类。

import requests
import bs4
res = requests.get('http://www.hindisamay.com/writer/%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF.cspx?id=1275&name=%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF')
print(res)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
h = soup.select('title')
print(h)
    #got all link of the website
all_links = soup.find_all("a")
for link in all_links:
    link.get("href")
    print(link.get("href"))

我用这个给所有的链接...

enter image description here

python python-3.x python-2.7 beautifulsoup scapy
2个回答
0
投票

要提取所有在 main-bullet 类,你可以使用 .select() 方法。

例如

import requests
from bs4 import BeautifulSoup

url = 'http://www.hindisamay.com/writer/%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF.cspx?id=1275&name=%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')

for a in soup.select('.main-bullet a'):
    print(a.get_text(strip=True), a['href'])

打印:

चुनी हुई कविताएँ http://www.hindisamay.com/content/714/1/अज्ञेय-कविताएँ-चुनी-हुई-कविताएँ.cspx 
उत्तर प्रियदर्शी http://www.hindisamay.com/content/499/1/अज्ञेय-नाटक-उत्तर-प्रियदर्शी.cspx 
वसंत का अग्रदूत http://www.hindisamay.com/content/493/1/अज्ञेय-संस्मरण-वसंत-का-अग्रदूत.cspx 
संवाद http://www.hindisamay.com/content/521/1/अज्ञेय-संस्मरण-संवाद.cspx 
‘अज्ञेय’ : अपनी निगाह में http://www.hindisamay.com/content/496/1/अज्ञेय-आत्मकथा-अज्ञेय-अपनी-निगाह-में.cspx 
ऋण-स्वीकारी हूँ http://www.hindisamay.com/content/495/1/अज्ञेय-आत्मकथा-ऋण-स्वीकारी-हूँ.cspx 
कला का स्वाभाव और उद्देश्य http://www.hindisamay.com/content/6777/1/अज्ञेय-निबंध-कला-का-स्वाभाव-और-उद्देश्य.cspx 
कविता : श्रव्य से पाठ्य तक http://www.hindisamay.com/content/2000/1/अज्ञेय-निबंध-कविता-श्रव्य-से-पाठ्य-तक.cspx 
काल का डमरु-नाद http://www.hindisamay.com/content/2001/1/अज्ञेय-निबंध-काल-का-डमरु-नाद.cspx 
छाया का जंगल http://www.hindisamay.com/content/2009/1/अज्ञेय-निबंध-छाया-का-जंगल.cspx 
ताली तो छूट गयी http://www.hindisamay.com/content/2007/1/अज्ञेय-निबंध-ताली-तो-छूट-गयी.cspx 
नई कविता : प्रयोग के आयाम http://www.hindisamay.com/content/2004/1/अज्ञेय-निबंध-नई-कविता-प्रयोग-के-आयाम.cspx 
भारतीयता http://www.hindisamay.com/content/6775/1/अज्ञेय-निबंध-भारतीयता.cspx 
मरुथल की सीपियाँ http://www.hindisamay.com/content/2008/1/अज्ञेय-निबंध-मरुथल-की-सीपियाँ.cspx 
रूढ़ि और मौलिकता http://www.hindisamay.com/content/6778/1/अज्ञेय-निबंध-रूढ़ि-और-मौलिकता.cspx 
वैज्ञानिक सत्ता, मिथकीय सत्ता और कवि http://www.hindisamay.com/content/1999/1/अज्ञेय-निबंध-वैज्ञानिक-सत्ता-मिथकीय-सत्ता-और-कवि.cspx 
वासुदेव प्याला http://www.hindisamay.com/content/4299/1/अज्ञेय-निबंध-वासुदेव-प्याला.cspx 
शब्द, मौन, अस्तित्व http://www.hindisamay.com/content/2005/1/अज्ञेय-निबंध-शब्द-मौन-अस्तित्व.cspx 
स्मृति और काल http://www.hindisamay.com/content/2002/1/अज्ञेय-निबंध-स्मृति-और-काल.cspx 

... and so on.

EDIT(保存为csv)。

import csv
import requests
from bs4 import BeautifulSoup

url = 'http://www.hindisamay.com/writer/%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF.cspx?id=1275&name=%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF'

soup = BeautifulSoup(requests.get(url).content, 'html.parser')

all_data = []
for a in soup.select('.main-bullet a'):
    all_data.append([a.get_text(strip=True), a['href']])

with open('data.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
    for row in all_data:
        writer.writerow(row)

这将产生 data.csv (截图来自 LibreOffice)。

enter image description here


0
投票

你可以使用 .xpath() 方法。例如,下面的代码返回的是 "main-bullet "类下提取的所有链接的列表。

import requests
from lxml import html
response = requests.get('http://www.hindisamay.com/writer/%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF.cspx?id=1275&name=%E0%A4%85%E0%A4%9C%E0%A5%8D%E0%A4%9E%E0%A5%87%E0%A4%AF')
response_html = html.fromstring(response.content.decode('utf8'))
links = response_html.xpath('//ul[contains(@class,"main-bullet")]//a/@href')
links


-1
投票

你知道xpath?XPath

//*[@class="xxx"]
© www.soinside.com 2019 - 2024. All rights reserved.