V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Lwf1995
V2EX  ›  Python

求助:scrapy 利用 xpath 无法获取 img 的完整 src

  •  
  •   Lwf1995 · 2017-12-05 22:53:13 +08:00 · 4973 次点击
    这是一个创建于 2295 天前的主题,其中的信息可能已经有所发展或是发生改变。
    主要 py 文件码源:
    import scrapy
    from myfirst.items import MyfirstItem


    class TestSpider(scrapy.Spider):
    name = 'test'
    part_url = 'https://www.qiushibaike.com/imgrank/'
    page = 'page/'

    def start_requests(self):
    for page_number in range(1, 2, 1):
    if page_number == 1:
    yield self.make_requests_from_url(self.part_url)
    else:
    yield self.make_requests_from_url(self.part_url + self.page + str(page_number) + "/")

    def parse(self, response):
    item = MyfirstItem()
    item["img_url"] = response.xpath("//div[@class='thumb']/a/img/@src")
    for url in item["img_url"]:
    print(url)


    url 打印出来:
    <Selector xpath="//div[@class='thumb']/a/img/@src" data='//pic.qiushibaike.com/system/pictures/11'>
    实际地址比 data 的要长
    8 条回复    2019-04-08 19:10:43 +08:00
    rabbbit
        1
    rabbbit  
       2017-12-05 23:04:46 +08:00   ❤️ 1
    太长时间没用不太记得了
    试试 response.xpath("//div[@class='thumb']/a/img/@src").extract()
    Lwf1995
        2
    Lwf1995  
    OP
       2017-12-05 23:14:16 +08:00
    是的,谢谢,我来查查 extract()
    simp1e
        3
    simp1e  
       2017-12-05 23:28:12 +08:00
    去掉最前面两个斜杠。。

    print (url[2:])
    simp1e
        4
    simp1e  
       2017-12-05 23:31:00 +08:00
    @simp1e 看错了。。
    likeshu
        5
    likeshu  
       2017-12-06 09:38:02 +08:00
    XPATH
    mrzys
        6
    mrzys  
       2017-12-12 14:32:29 +08:00
    最简单的方法,f12,在 elements 右键,然后找到 copy,在找到 copy xpath
    qq583708076
        7
    qq583708076  
       2017-12-14 10:45:16 +08:00
    response.xpath("//div[@class='thumb']/a/img/@src") .extract_first()
    lp77885544
        8
    lp77885544  
       2019-04-08 19:10:43 +08:00
    楼主最后怎么解决的呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5381 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 06:53 · PVG 14:53 · LAX 23:53 · JFK 02:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.