【问题标题】:I want to extract the html from a particular div class named "se_component_wrap sect_dsc __se_component_area"我想从名为“se_component_wrap sect_dsc __se_component_area”的特定 div 类中提取 html
【发布时间】:2020-01-10 04:02:57
【问题描述】:

我已经有了一个工作 python 脚本,但我想自动从页面获取 url。我只需要 div 类 se_component_wrap sect_dsc __se_component_area 中的所有 html 代码,但目前我正在获取整个 html页面

from lxml import html
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
from fetch import *

my_url=('https://m.post.naver.com/viewer/postView.nhn?volumeNo=20163796&memberNo=29747755')
#Opening Client
uClient = uReq(my_url)
#Opening the client
page_html = uClient.read()
#Closing connection
uClient.close()

page_soup = soup(page_html, "html.parser")
clear_file=page_soup.prettify()
with open("test.txt","w", encoding="utf-8") as outp:
    outp.write(clear_file)
print (page_soup)

fetcher()

我希望输出是包含在该部门中的 html 代码,而不是完整的页面

【问题讨论】:

  • 即输出页面中的文字..我要分区的html代码..
  • 您的绳子目前设计用于刮掉整个页面。你想要什么部门标签?目标页面上有100个划分标签。
  • se_component_wrap sect_dsc __se_component_area
  • 虽然我想要那个部门标签内的所有代码..

标签: html python-3.x beautifulsoup python-requests lxml


【解决方案1】:

直接的方法是:

soup = BeautifulSoup(page_html, "html.parser")    
for div in soup.findAll('div',{'class':'se_component_wrap sect_dsc __se_component_area'}):
        print div

【讨论】:

  • ediv 是什么?是汤的功能吗
  • 对不起,这是一个错字。我已经编辑了我的答案。
  • 它不打印任何东西..我将它与其他类名一起使用..它只打印了行
  • 确保你用页面内容定义你的汤变量。我再次编辑了代码。在您现有的代码中适当地使用它。它应该工作
  • python from lxml import html from bs4 import BeautifulSoup from urllib.request import urlopen as uReq from fetch import * my_url=('https://m.post.naver.com/viewer/postView.nhn?volumeNo=20163796&memberNo=29747755') #Opening Client uClient = uReq(my_url) #Opening the client page_html = uClient.read() #Closing connection uClient.close() soup = BeautifulSoup(page_html, "html.parser") for div in soup.findAll('div',{'class':'se_component_wrap sect_dsc __se_component_area'}): print (div) 还是不行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-05
  • 2015-01-20
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多