Ygh6688 发表于 2020-12-17 10:35:30

正则搜索出来的数据只能加载一条

本帖最后由 Ygh6688 于 2020-12-17 10:41 编辑

<div class="blockcode"><blockquote>import requests
import re
from lxml import etree

def get_url(url):
    headers = {
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'

    }
    page_text = requests.get(url,headers=headers).text
    return page_text


def get_page(html):
    tittle = re.compile(r'<ul class="gl-warp.*?class="p-price">.*?<i>(\d+).\d+</i>.*?class="p-name.*?<em>([^¥^/^-].*?)</em>',re.S)

    # tittle = re.compile('<i>(\d+).\d+</i>',re.S)
    # tittle = re.compile('<em>([^¥^/^-].*?)</em>',re.S)
    shangpin = re.findall(tittle,html)
    for i in shangpin:
      yield{
      '价格':i,
      '商品':i,
      }
    print(shangpin)


def write_file(team):
      pass


def main():
    url = 'https://list.jd.com/list.html?cat=9987,653,655'
    html = get_url(url)
    team = get_page(html)
    for i in team:
      write_file(i)


if __name__ == "__main__":
      main()



如果我正常分开的打印价格和商品名称就可以出来所有信息,但是合并在一起就只能打印出一条信息求求大佬指出是我正则的的问题还是其他的


suchocolate 发表于 2020-12-17 15:54:24

import requests
from lxml import etree


def main():
    url = 'https://list.jd.com/list.html?cat=9987,653,655'
    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) '
                           'Chrome/72.0.3626.121 Safari/537.36'}
    r = requests.get(url, headers=headers)
    result = []
    html = etree.HTML(r.text)
    lis = html.xpath('//li[@class="gl-item"]')
    for li in lis:
      p_name = li.xpath('./div/div/a/em/text()')
      p_price = li.xpath('./div/div/strong/i/text()')
      print(p_name, p_price)


if __name__ == "__main__":
    main()

Ygh6688 发表于 2020-12-17 16:37:32

suchocolate 发表于 2020-12-17 15:54


页: [1]
查看完整版本: 正则搜索出来的数据只能加载一条