好友
阅读权限10
听众
最后登录1970-1-1
|
本帖最后由 wushaominkk 于 2019-3-20 16:40 编辑
不想麻烦点击网页,直接获取有关内容
导入模块
[Python] 纯文本查看 复制代码 from lxml import etree
import requests
定义函数入口
if __name__ == '__main__':
main()
发送请求获取内容
def handle_request(url):
heades = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134',
}
response = requests.get(url=url,headers = heades).text
return response
主函数,获取内容并显示
def main():
url = 'https://voice.hupu.com/nba'
content = handle_request(url)
news = etree.HTML(content)
news_content = news.xpath(r'//div[@class="news-list"]/ul/li/div/h4/a/text()')
news_url = news.xpath(r'//div[@class="news-list"]/ul/li/div/h4/a/@href')
for new in zip(news_content ,news_url):
print(new)
代码全貌
from lxml import etree
import requests
def handle_request(url):
heades = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134',
}
response = requests.get(url=url,headers = heades).text
return response
def main():
url = 'https://voice.hupu.com/nba'
content = handle_request(url)
news = etree.HTML(content)
news_content = news.xpath(r'//div[@class="news-list"]/ul/li/div/h4/a/text()')
news_url = news.xpath(r'//div[@class="news-list"]/ul/li/div/h4/a/@href')
for new in zip(news_content ,news_url):
print(new)
if __name__ == '__main__':
main()
结果展示
('利拉德:已做好CJ缺席剩余常规赛打算,不想他仓促复出', 'https://voice.hupu.com/nba/2407460.html')
('小洛佩斯:队友让我处于有利位置,看到熟悉面孔感觉很棒', 'https://voice.hupu.com/nba/2407458.html')
('汤普森:不放弃投篮机会,投进一球就能找回手感', 'https://voice.hupu.com/nba/2407457.html')
('金特里:这些比赛对我们的年轻球员来说都是非常好的经历', 'https://voice.hupu.com/nba/2407455.html')
('艾顿谈防守洛佩斯:他在低位的传统勾手简直不可阻挡', 'https://voice.hupu.com/nba/2407454.html')
('东契奇:我也想让自己的名字被人们记住,我想打很长时间', 'https://voice.hupu.com/nba/2407451.html')
('杰夫-范甘迪:吉诺比利是我最欣赏的球员', 'https://voice.hupu.com/nba/2407450.html')
('十佳球:德克总得分超越张伯伦,库里后场压哨三分', 'https://voice.hupu.com/nba/2407449.html')
('德克:我儿子最喜欢鲍威尔和梅杰里,就因为他们经常扣篮', 'https://voice.hupu.com/nba/2407444.html')
('诺维茨基:投篮并拉开空间,这对我的技术风格来说很完美', 'https://voice.hupu.com/nba/2407442.html')
('布罗格登成为第8位跻身180俱乐部的球员', 'https://voice.hupu.com/nba/2407440.html')
('东契奇:任何发生在诺维茨基身上的好事都是他应得的', 'https://voice.hupu.com/nba/2407439.html')
......
|
免费评分
-
参与人数 1 | 吾爱币 +2 |
热心值 +1 |
收起
理由
|
苏紫方璇
| + 2 |
+ 1 |
欢迎分析讨论交流,吾爱破解论坛有你更精彩! |
查看全部评分
|
发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。 |
|
|
|
|