使用python beautifulsoup进行Web爬行

使用python beautifulsoup进行Web爬行,python,html,beautifulsoup,Python,Html,Beautifulsoup,如何提取位于命名类下的段落标记和中的数据?使用以下函数和: 导入请求 从bs4导入BeautifulSoup url=“…” r=请求。获取(url) 数据=r.text soup=BeautifulSoup(数据'html.parser') div=soup.find('div',{'class':'class-name'}) ps=div.find_all('p')) lis=div.find_all('li')) #打印所有标签的内容 对于ps中的p: 打印(p.text) #打印所有标记

如何提取位于命名类下的
段落标记和
  • 中的数据?

    使用以下函数和:

    导入请求
    从bs4导入BeautifulSoup
    url=“…”
    r=请求。获取(url)
    数据=r.text
    soup=BeautifulSoup(数据'html.parser')
    div=soup.find('div',{'class':'class-name'})
    ps=div.find_all('p'))
    lis=div.find_all('li'))
    #打印所有标签的内容
    对于ps中的p:
    打印(p.text)
    #打印所有
  • 标记的内容 对于lis中的li: 打印(li.text)
  • 使用功能和:

    导入请求
    从bs4导入BeautifulSoup
    url=“…”
    r=请求。获取(url)
    数据=r.text
    soup=BeautifulSoup(数据'html.parser')
    div=soup.find('div',{'class':'class-name'})
    ps=div.find_all('p'))
    lis=div.find_all('li'))
    #打印所有标签的内容
    对于ps中的p:
    打印(p.text)
    #打印所有
  • 标记的内容 对于lis中的li: 打印(li.text)
  • post a sample input.post sample html/xmlpost a sample input.post sample html/xmlAwesome..非常感谢:-)非常感谢..非常感谢:-)
    import requests
    from bs4 import BeautifulSoup
    
    url = '...'
    
    r = requests.get(url)
    data = r.text
    soup = BeautifulSoup(data, 'html.parser')
    
    div = soup.find('div', {'class':'class-name'})
    ps = div.find_all('p')
    lis = div.find_all('li')
    
    # print the content of all <p> tags
    for p in ps:
        print(p.text)
    
    # print the content of all <li> tags
    for li in lis:
        print(li.text)