作业笔记03_request&bs4

2017-02-07  本文已影响45人  ChZ_CC
  1. http://www.pythonscraping.com/pages/warandpeace.html

    • 提取以上网页中所有的对话(显示为红色字体)
  2. http://www.pythonscraping.com/pages/page3.html

    • 提取以上网页中礼物标题(title)和礼物花费(cost)
    • 查询礼物“Dead Parrot” 的花费(cost)
import requests
from bs4 import BeautifulSoup

url_1 = 'http://www.pythonscraping.com/pages/warandpeace.html'
url_2 = 'http://www.pythonscraping.com/pages/page3.html'

r1 = requests.get(url_1)
r2 = requests.get(url_2)

soup1 = BeautifulSoup(r1.content, 'html.parser')
soup2 = BeautifulSoup(r2.content, 'html.parser')

# 1)提取以上网页中所有的对话(显示为红色字体)
redText = soup1.find_all("span", {"class":"red"})
for item in redText:
    print(item.text)

# 2)提取以上网页中礼物标题(title)和礼物花费(cost)
# title
items = soup2.find_all('tr',{"class":"gift"})
for item in items:
    print(item.td.text)

#cost
for item in items:
    print(item.contents[2].text)
上一篇下一篇

猜你喜欢

热点阅读