Python中的Web Scraping问题:返回空值

时间:2018-01-10 12:33:28

标签: python html web-scraping

我是Python新手。当我在Python中运行以下代码进行Web抓取时,我得到一个空值。我想从指定的网址打印比特币价格。请帮忙。

`import bs4`
 import requests
 url='https://coinmarketcap.com/'
 res=requests.get(url)
 soup = bs4.BeautifulSoup(res.text,'html.parser')

 element=soup.select('html.js.video.videoautoplay body div.container div.row div.col-lg-10 div.row div.col-xs-12 div.table-fixed-column-mobile.compact-name-column div#currencies_wrapper.dataTables_wrapper.no-footer table#currencies.table.dataTable.no-footer tbody tr#id-bitcoin.odd td.no-wrap.text-right a.price')
    print(element)

1 个答案:

答案 0 :(得分:1)

您不必遵循整个html结构,只需选择包含所需数据的项目即可。

import bs4
import requests

url = 'https://coinmarketcap.com/'
res = requests.get(url)
soup = bs4.BeautifulSoup(res.text,'html.parser')
element = soup.select_one('tr#id-bitcoin a.price').text

print(element)
  

$ 14122.10