[求助] 如何提取出网页标签内所有的属性值

2018-12-22 12:28:14 +08:00
 15874103329

代码是这样的,要如何改动啊

import requests

from pyquery import PyQuery as pq

from urllib.parse import urlencode

import re

def dizhi():

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3573.0 Safari/537.36'}

data = {
    'q': '微信群',
    'typeall': '1',
    'suball': '1',
    'timescope': 'custom:2018 - 12 - 20 - 0: 2018 - 12 - 22 - 0',
    'Refer': 'g'
}

url = 'https://s.weibo.com/weibo/%25E5%25AE%259D%25E5%25A6%2588%25E7%25BE%25A4?' + urlencode(data)

wangzhi = requests.get(url,headers = headers)

return wangzhi.text

def jiexi(html):

doc = pq(html)

item = doc('.m3 li')

print(item('img').attr('src'))

def main():

html = dizhi()

jiexi(html)

if name == 'main':

main()

打印结果:

//ww4.sinaimg.cn/thumb150/475ee913ly1fydb7js7inj20orcmvx6q.jpg

1358 次点击
所在节点    问与答
4 条回复
15874103329
2018-12-22 12:29:43 +08:00
打印 item 有很多图片,但是获取属性值只打印出了一个,如何将 item 中所有的图片打印出来啊
ClutchBear
2018-12-22 12:53:53 +08:00

item 所有的图片,
你要遍历才行
15874103329
2018-12-22 15:59:49 +08:00
@ClutchBear 哦哦,谢谢大佬
dreambig183
2018-12-22 20:59:31 +08:00
推荐用 scrapy 的 selector.或是直接用 scrapy 框架吧,真的很方便!!!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/519974

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX