簡體   English   中英

web怎么刮<p>里面的標簽</p><div>具有來自 HTML 的類/ID 的標簽,使用 Python</div><div id="text_translate"><p> 我想提取數據,例如</p><blockquote><p>發布日期:2016 年 6 月 16 日 漏洞標識符:APSB16-23 優先級:3 CVE 編號:CVE-2016-4126</p></blockquote><p> 來自<em><a href="https://helpx.adobe.com/security/products/air/apsb16-23.ug.html" rel="nofollow noreferrer">https://helpx.adobe.com/security/products/air/apsb16-23.ug.html</a></em></p><p> 編碼:</p><pre> import requests from bs4 import BeautifulSoup as bs from pprint import pprint r = requests.get('https://helpx.adobe.com/cy_en/security/products/air/apsb16-31.html') soup = bs(r.content, 'html.parser') pprint([i.text for i in soup.select('div &gt;.text &gt; p', limit = 4 )] )</pre><p> output:</p><pre> ['Release date:\xa0September 13, 2016', 'Vulnerability identifier: APSB16-31', 'Priority: 3', 'CVE number:\xa0CVE-2016-6936']</pre><p> 問題是 /xa0。 我應該如何刪除它? 如果還有其他有效的代碼嗎? 我也想把它轉換成 CSV 文件。 謝謝你。</p></div>

[英]How to web scraping <p> tags inside <div> tags that has class/id from HTML using Python

我想提取數據,例如

發布日期:2016 年 6 月 16 日 漏洞標識符:APSB16-23 優先級:3 CVE 編號:CVE-2016-4126

來自https://helpx.adobe.com/security/products/air/apsb16-23.ug.html

編碼:

import requests
from bs4 import BeautifulSoup as bs
from pprint import pprint
    
r = requests.get('https://helpx.adobe.com/cy_en/security/products/air/apsb16-31.html')
soup = bs(r.content, 'html.parser')
pprint([i.text for i in soup.select('div > .text >  p' , limit = 4 )] )

output:

['Release date:\xa0September 13, 2016',
 'Vulnerability identifier: APSB16-31',
 'Priority: 3',
 'CVE number:\xa0CVE-2016-6936']

問題是 /xa0。 我應該如何刪除它? 如果還有其他有效的代碼嗎? 我也想把它轉換成 CSV 文件。 謝謝你。

\xa0實際上是 Latin1 (ISO 8859-1) 中的不間斷空格,也是 chr(160)。 您應該用空格替換它。

嘗試這個:

import requests
from bs4 import BeautifulSoup as bs
from pprint import pprint

r = requests.get('https://helpx.adobe.com/cy_en/security/products/air/apsb16-31.html')
soup = bs(r.content, 'html.parser')
pprint([i.text.replace(u'\xa0', u' ') for i in soup.select('div > .text >  p', limit=4)])

Output:

['Release date: September 13, 2016',
 'Vulnerability identifier: APSB16-31',
 'Priority: 3',
 'CVE number: CVE-2016-6936']

編輯:要將結果放到.csv文件中,請使用pandas

就是這樣:

import pandas as pd
import requests
from bs4 import BeautifulSoup as bs

r = requests.get('https://helpx.adobe.com/cy_en/security/products/air/apsb16-31.html')
soup = bs(r.content, 'html.parser')
release = [
    i.getText().replace(u'\xa0', u' ').split(": ") for i
    in soup.select('div > .text >  p', limit=4)
]
pd.DataFrame(release).set_index(0).T.to_csv("release_data.csv", index=False)

Output:

在此處輸入圖像描述

我剛剛使用了您的代碼並在提取的 HTML 標記上添加了一個 for 循環。 似乎在使用列表理解時 unicode 轉換器不存在。 雖然它只是一個假設。

至於我剛剛即興創作的劇本。

import requests
from bs4 import BeautifulSoup
from pprint import pprint

url = "https://helpx.adobe.com/cy_en/security/products/air/apsb16-31.html"
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
data = [i for i in soup.select('div > .text >  p', limit=4)]

for i in data:
    print(i.text)
    print("-"*20)

這將為您提供所需的 output。 查看圖像的鏈接,因為它不會在此處顯示圖像本身。 在此處輸入圖像描述

web如何抓取特定標簽<p>在</p><div>使用 HTML 中的 Python</div><div id="text_translate"><p> 我要提取的數據來自這個網站<strong><a href="https://www.adobe.com/support/security/advisories/apsa11-04.html" rel="nofollow noreferrer">https://www.adobe.com/support/security/advisories/apsa11-04.html</a></strong> 。 我只想提取</p><blockquote><p>發布日期:2011 年 12 月 6 日 最后更新時間:2012 年 1 月 10 日 漏洞標識符:APSA11-04 CVE 編號:CVE-2011-2462</p></blockquote><p> 編碼:</p><pre> from bs4 import BeautifulSoup div = soup.find("div", attrs={"id": "L0C1-body"}) for p in div.findAll("p"): if p.find('strong'): print(p.text)</pre><p> output:</p><pre> Release date: December 6, 2011 Last updated: January 10, 2012 Vulnerability identifier: APSA11-04 CVE number: CVE-2011-2462 Platform: All *Note: Adobe Reader for Android and Adobe Flash Player are not affected by this issue.</pre><p> 我不想要這些信息。 我應該如何過濾它?</p><blockquote><p> 平台:全部 *注意:用於 Android 的 Adobe Reader 和 Adobe Flash Player 不受此問題的影響。</p></blockquote></div>

[英]How to web scraping specific tags <p> in <div> using Python from HTML

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 web如何抓取特定標簽<p>在</p><div>使用 HTML 中的 Python</div><div id="text_translate"><p> 我要提取的數據來自這個網站<strong><a href="https://www.adobe.com/support/security/advisories/apsa11-04.html" rel="nofollow noreferrer">https://www.adobe.com/support/security/advisories/apsa11-04.html</a></strong> 。 我只想提取</p><blockquote><p>發布日期:2011 年 12 月 6 日 最后更新時間:2012 年 1 月 10 日 漏洞標識符:APSA11-04 CVE 編號:CVE-2011-2462</p></blockquote><p> 編碼:</p><pre> from bs4 import BeautifulSoup div = soup.find("div", attrs={"id": "L0C1-body"}) for p in div.findAll("p"): if p.find('strong'): print(p.text)</pre><p> output:</p><pre> Release date: December 6, 2011 Last updated: January 10, 2012 Vulnerability identifier: APSA11-04 CVE number: CVE-2011-2462 Platform: All *Note: Adobe Reader for Android and Adobe Flash Player are not affected by this issue.</pre><p> 我不想要這些信息。 我應該如何過濾它?</p><blockquote><p> 平台:全部 *注意:用於 Android 的 Adobe Reader 和 Adobe Flash Player 不受此問題的影響。</p></blockquote></div> 用beautifulsoup刮文章:刮 <p> 里面的標簽 <div > 帶有ID的標簽 Web 刮擦阻隔器<p>標簽</p> 涉及帶有屬性的HTML標記的Python Web抓取 Python BeautifulSoup - 抓取 Div Span 和 p 標簽 - 以及如何獲得 div 名稱的精確匹配 如何<a href>從多個</a>中找到一個<p>標簽和一個 div class</p> Web 使用 python 抓取多個標簽 如何從結果中刪除元素標簽,Web 用 Python 刮文章 使用BeautifulSoup和Python從多個標簽(例如具有類的h1和p標簽)中提取文本 使用Selenium和python提取沒有任何類和ID的div標簽
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM