如果您想将About the author与不在段落中的内容一起踢出,可以通过在类td-post-content内打印p标记下的span标记的内容来完成。为了简洁起见,我在本例中使用选择器。也试试下面的方法。在
import requests
from bs4 import BeautifulSoup
url = 'https://www.the-blockchain.com/2018/06/29/mcafee-labs-report-6x-increase-in-crypto-mining-malware-incidents-in-q1-2018/'
res = requests.get(url,headers={"User-Agent":"defined"})
soup = BeautifulSoup(res.text, 'lxml')
paragraph = [p.text for p in soup.select('.td-post-content p span')]
print(paragraph)
import requests
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'}
r = requests.get('https://www.the-blockchain.com/2018/06/29/mcafee-labs-report-6x-increase-in-crypto-mining-malware-incidents-in-q1-2018/', headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
container = soup.find('div', class_='td-post-content')
for para in container.find_all('p', recursive=False):
print(para.text)
如果您想将
About the author
与不在段落中的内容一起踢出,可以通过在类td-post-content
内打印p
标记下的span
标记的内容来完成。为了简洁起见,我在本例中使用选择器。也试试下面的方法。在您需要使用selenium,因为我试图用
requests
来完成,但它不起作用,因为数据是用javascript
加载的,后面跟着bs4
输出
^{pr2}$您需要的所有段落都位于}标记不是直接子标记(它们嵌套在其他
<div class="td-post-content">
标记内,以及作者信息的段落。但是,必需的<p>
标记是此<div>
标记的直接子标记,而其他不需要的{div
标记中)。在因此,您只能使用^{} 来访问这些标记。在
代码:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐