pythonは豆弁網が上映中の上位15名の映画の採点を獲得した.

2035 ワード

2018.12.23更新:anacondaがインストールしたlxmlライブラリを使用している場合は、python 3に直接次のコードを変更する必要があります.7+pycharmならこちらのコードは問題なく実行できます. 
from lxml import html
#      import etree
...
...

htmlelement = html.etree.HTML(text)
#           ,HTML        

#     ,       。            

 
import requests                    #    request  lxml 
from lxml import etree

headers = {
    'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK\
    it/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
    'Referer':'https://www.baidu.com/s?ie=UTF-8&wd=douban'
}
url = "https://movie.douban.com/cinema/nowplaying/nanchang/"
#          url,          


res = requests.get(url)
text = res.text

html = etree.HTML(text)
ul = html.xpath("//ul[@class='lists']")[0]
lis = ul.xpath("./li")
for li in lis:
    title = li.xpath("@data-title")[0]
    score = li.xpath("@data-score")[0]
    if score == "0":
        score = "    "
    print("   :%s"%title)
    print("    :%s"%score)
    print("")


改良版:
import requests
from lxml import etree

headers = {
    'User_Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebK\
    it/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
    'Referer':'https://www.baidu.com/s?ie=UTF-8&wd=douban'
}
urlori = "https://movie.douban.com/cinema/nowplaying/"
location = input("         ")
url = urlori+location
print(url)
res = requests.get(url)
text = res.text

html = etree.HTML(text)
ul = html.xpath("//ul[@class='lists']")[0]
lis = ul.xpath("./li")
for li in lis:
    title = li.xpath("@data-title")[0]
    score = li.xpath("@data-score")[0]
    if score == "0":
        score = "    "
    print("   :%s"%title)
    print("    :%s"%score)
    print("")