python - 无法使用 beautifulsoup 3 从 json 脚本中提取所有 URL

标签 python json web-scraping beautifulsoup export-to-csv

   import requests
from bs4 import BeautifulSoup
import json
import re

url = "https://www.daraz.pk/catalog/?q=dell&_keyori=ss&from=input&spm=a2a0e.searchlist.search.go.57446b5079XMO8"
page = requests.get(url)

print(page.status_code)
print(page.text)
soup = BeautifulSoup(page.text, 'html.parser')
print(soup.prettify())



alpha = soup.find_all('script',{'type':'application/ld+json'})

jsonObj = json.loads(alpha[1].text)

下面是从json对象中查找所有相关产品信息的代码

for item in jsonObj['itemListElement']:
    name = item['name']
    price = item['offers']['price']
    currency = item['offers']['priceCurrency']
    availability = item['offers']['availability'].split('/')[-1]
    availability = [s for s in re.split("([A-Z][^A-Z]*)", availability) if s]
    availability = ' '.join(availability)

这是提取 json 脚本 URL 的代码

    url = item['url']

print('Availability: %s  Price: %0.2f %s   Name: %s' %(availability,float(price), currency,name, url))

下面是从 csv 中提取数据的代码:

outfile = open('products.csv','w', newline='')
writer = csv.writer(outfile)
writer.writerow(["name", "type", "price", "priceCurrency", "availability" ])

alpha = soup.find_all('script',{'type':'application/ld+json'})

jsonObj = json.loads(alpha[1].text)

for item in jsonObj['itemListElement']:
    name = item['name']
    type = item['@type']
    url = item['url']
    price = item['offers']['price']
    currency = item['offers']['priceCurrency']
    availability = item['offers']['availability'].split('/')[-1]

文件创建 header ,但 CSV 中没有 URL 的数据

writer.writerow([name, type, price, currency, availability, URL ])
outfile.close()

最佳答案

首先,您不在那里包含标题。没什么大不了的,只是第一行的 url 列中的标题有一个空白。因此,包括:

writer.writerow(["name", "type", "price", "priceCurrency", "availability", "url" ]) 

其次,将字符串存储为 url,然后在编写器中引用 URLURL 没有任何值。事实上,它应该给出 URL is not Defined 或类似错误。

由于您已在代码中使用 url ,其中 url = "https://www.daraz.pk/catalog/?q=dell&_keyori=ss&from=input&spm=a2a0e.searchlist .search.go.57446b5079XMO8",我也可能会将变量名称更改为 url_text 之类的名称。

我可能还会使用变量 type_texttype 以外的其他变量,因为 type 是 python 中的内置函数。

但是你需要更改为:

writer.writerow([name, type, price, currency, availability, url ])
outfile.close()

完整代码:

import requests
from bs4 import BeautifulSoup
import json
import csv

url = "https://www.daraz.pk/catalog/?q=dell&_keyori=ss&from=input&spm=a2a0e.searchlist.search.go.57446b5079XMO8"
page = requests.get(url)

print(page.status_code)
print(page.text)
soup = BeautifulSoup(page.text, 'html.parser')
print(soup.prettify())



alpha = soup.find_all('script',{'type':'application/ld+json'})

jsonObj = json.loads(alpha[1].text)

outfile = open('c:\products.csv','w', newline='')
writer = csv.writer(outfile)
writer.writerow(["name", "type", "price", "priceCurrency", "availability" , "url"])

for item in jsonObj['itemListElement']:
    name = item['name']
    type_text = item['@type']
    url_text = item['url']
    price = item['offers']['price']
    currency = item['offers']['priceCurrency']
    availability = item['offers']['availability'].split('/')[-1]

    writer.writerow([name, type_text, price, currency, availability, url_text ])

outfile.close()

关于python - 无法使用 beautifulsoup 3 从 json 脚本中提取所有 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54088382/

相关文章:

python - 如何像使用嵌套 for 循环一样迭代任意数量的列表?

python - 为什么我的正则表达式也接受字母? ^[0-9\-\+_]

python - 如何在 Django 中使用装饰器保存发布数据

python - 抓取无限滚动页面

python - 如何四舍五入一个值但不以单位步长

java - 如何在 Java 中将新的类对象添加到现有的 JSON 文件中?

ios - 如何将模型对象序列化为 JSON 字符串 - Swift 3

java - 使用 Google Gson 创建 json

python - 抓取一个遍历网站页面的表格 : how to define the last page?

python - 使用 Beautiful Soup 保存实体进行抓取