马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 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[0],
'商品':i[1],
}
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()
如果我正常分开的打印价格和商品名称就可以出来所有信息,但是合并在一起就只能打印出一条信息求求大佬指出是我正则的的问题还是其他的
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[4]/a/em/text()')[0]
p_price = li.xpath('./div/div[3]/strong/i/text()')[0]
print(p_name, p_price)
if __name__ == "__main__":
main()
|