| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
用xpath抓数据,在浏览器上显示能抓到,但是到Python里面返回空列表的情况吗 
all_url的值是空 
 
 
import requests 
from fake_useragent import UserAgent 
from lxml import etree 
 
 
def get_html(index_url): 
    headers={ 
        "User-Agent":UserAgent().chrome 
    } 
    resp=requests.get(index_url,headers=headers) 
    resp.encoding='gbk' 
    if resp.status_code==200: 
        return resp.text 
    else: 
        return None 
def parse_index(html): 
 
    e=etree.HTML(html) 
 
    all_url=e.xpath('//div[@class="channel-detail movie-item-title"]/a/@href') 
    return ['https://maoyan.com{}'.format(url)for url in all_url] 
 
def main(): 
    index_url="https://maoyan.com/films" 
    html=get_html(index_url) 
 
    moive_url=parse_index(html) 
    print(moive_url) 
 
 
if __name__ == '__main__': 
    main() |   
 
 
 
 |