Python 网络刮擦。美丽的乌苏。多页。你到底是怎么做到的?

Python 网络刮擦。美丽的乌苏。多页。你到底是怎么做到的?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,嗨,我是一个编程新手。所以我花了4天时间学习python。我也用了一些新的脏话。 我特别感兴趣的是,作为一种练习,尝试一些网络抓取来学习一些新的东西,并获得一些曝光,看看它是如何工作的。 这就是我想到的。请参见末尾的代码。它在一定程度上起作用 但是缺少什么呢 这个网站有页码。在本例中,您将如何添加到这个脚本并让python继续查看其他页面并执行相同的刮取操作。刮第一页,刮第二页,刮第三页。。。11并将结果发布到csv? 8、9、10和11 在这些页面上,图像实际上是一个缩略图像,大约251px

嗨,我是一个编程新手。所以我花了4天时间学习python。我也用了一些新的脏话。 我特别感兴趣的是,作为一种练习,尝试一些网络抓取来学习一些新的东西,并获得一些曝光,看看它是如何工作的。 这就是我想到的。请参见末尾的代码。它在一定程度上起作用

但是缺少什么呢

这个网站有页码。在本例中,您将如何添加到这个脚本并让python继续查看其他页面并执行相同的刮取操作。刮第一页,刮第二页,刮第三页。。。11并将结果发布到csv?

8、9、10和11

在这些页面上,图像实际上是一个缩略图像,大约251px x 251px。 你会如何加入到这个脚本中去呢。当你在它下面的时候,按照详细产品页面的链接,从那里捕获图像链接,其中图像是1600px乘以1600px,并将这些链接发布到CSV

当我们确定了这些链接后,让我们也将这些较大的图像下载到一个文件夹中

CSV作者。我也不明白第58行 因为我在23区 如果不计算产品数量,我怎么知道有多少产品,即第一页上有24种产品

这就是我想学的。不要求太多,他讽刺地说,我可以付钱让别人做这件事,但这其中的乐趣何在?这并没有教会我如何“钓鱼”

哪里是学习python的好地方?关于网页抓取的大师班。这似乎是反复试验和博客文章,你可以从中获取一些信息,把它们拼凑在一起。 也许我需要一位导师。 我真希望有人能和我联系,告诉我美丽的乌苏是怎么回事。通过反复试验,主要是猜测,得出结论。不了解它,但它只是工作

无论如何,如果你能帮我把这一切结合起来,写出一个像样的剧本,我将不胜感激。 希望有人不介意帮助我

向organicwine致歉,因为他们将网站用作学习工具。我不希望对现场造成任何伤害或滋扰

先谢谢你 约翰

代码:


至于您关于如何浏览多页的问题。 您不应该硬编码包含查询的URL,如?pgnum=1。因为它是遍历所有页面的平均值


例如,您只需在当前N页上执行您需要的任何操作,然后请求…?pgnum=N+1。

通过将页码放入URL来创建URL,然后将其余代码放入for循环,您可以使用lenwinenames来计算您有多少个结果。您应该在for循环之外编写。以下是您的代码和这些更改:

导入请求 导入csv 从bs4导入BeautifulSoup 页数=11 结果=[] 对于rangenum_页面中的pgnum: url=fhttps://www.organicwine.com.au/vegan/?pgnum={pgnum+1} response=requests.geturl 网站\u html=response.text soup=BeautifulSoupwebsite\u html,html.parser product_title=soup.find_alldiv,class_u=caption winename=[] 对于产品名称中的葡萄酒: winetext=wine.a.text winename.appendwinetext product\u price=soup.find\u alldiv,class\u=wrap thumb mob 价格=[] 对于产品价格中的葡萄酒: wineprice=wine.span.text 价格 图像=[] product\u image\u link=soup.find\u alldiv,class\u=缩略图 对于产品图片链接中的图片链接: winelink=imagelink.a[href] response=requests.getwinelink wine\u page\u soup=BeautifulSoupresponse.text,html.parser main\u image=wine\u page\u soup.finda,class\uu=fancybox image.appendmain_image['href'] 对于rangelenwinename中的i: 结果。追加[winename[i],price[i],image[i]] 使用open/tmp/organizeWine2.csv,w,newline=as文件: writer=csv.writerfile,分隔符=, writer.writerow[葡萄酒名称、葡萄酒价格、葡萄酒图像链接] writer.writerowsresults 下面是我如何重写你的代码来完成这个任务。它更像python,你基本上不应该写任何东西,总有一种更干净的方法,它不需要知道结果有多少页:

导入csv 进口itertools 导入时间 导入请求 从bs4导入BeautifulSoup 数据=[] 尝试打开最多100页,以防刮码被破坏 这可能是因为网站发生了变化。 对于范围为1100的pgnum: url=fhttps://www.organicwine.com.au/vegan/?pgnum={pgnum} response=requests.geturl 网站\u html=response.text soup=BeautifulSoupwebsite\u html,html.parser 搜索结果=soup.find\u alldiv,类=缩略图 对于搜索结果中的搜索结果: name=search\u result.finddiv,class\u=caption.a.text price=search\u result.findp,class\u=price.span.text 链接到prod uct的页面 link=search\u result.finddiv,class\ux=thumbnail image.a[href] 获取全分辨率产品图像 response=requests.getlink time.1速率限制 wine\u page\u soup=BeautifulSoupresponse.text,html.parser main\u image=wine\u page\u soup.finda,class\uu=fancybox image\u url=main\u image[href] 或者你可以从缩略图的URL中猜出来 缩略图=搜索结果.finddiv,类=缩略图图像.a.img['src'] image_url=缩略图。替换“/thumbL/”、“/full/” 数据。追加[名称、价格、链接、图像\u url] 如果当前页面上没有“下一页”按钮或没有搜索结果, 别刮了 如果不是soup.findi,class_fa=chevron right或not搜索结果: 打破 利率限制 时间1 使用open/tmp/organizeWine3.csv,w,newline=as文件: writer=csv.writerfile,分隔符=, writer.writerow[葡萄酒名称、葡萄酒价格、葡萄酒链接、葡萄酒图像链接] writer.writerowsdata
要获取页面,您可以在范围11:url=fhttps://www.organicwine.com.au/vegan/?pgnum={i+1}我认为你的问题更适合于了解你有多少葡萄酒可以做lenwinenameHi Boris,谢谢你花时间回答。范围部分和Lenwinename很棒,我可以使用它。同时感谢您对codereview.stackexchange.com社区的建议。我将在那里看一看并摆出这个姿势。谢谢。嗨,鲍里斯,谢谢你的帮助。我非常感谢你。为了让你知道结果,我联系了codereview.stackexchange.com,他们在那里如此粗鲁和无益……你应该知道代码。。。。!也许我只是运气不好,得到了这个星球上最无助的人。我会继续寻求更多的帮助和鼓励。
import requests
import csv
from bs4 import BeautifulSoup

URL = "https://www.organicwine.com.au/vegan/?pgnum=1"
response = requests.get(URL)
website_html = response.text

soup = BeautifulSoup(website_html, "html.parser")

product_title = soup.find_all('div', class_="caption")
# print(product_title)

winename = []
for wine in product_title:
    winetext = wine.a.text
    winename.append(winetext)
    print(f'''Wine Name: {winetext}''')
# print(f'''\nWine Name: {winename}\n''')

product_price = soup.find_all('div', class_='wrap-thumb-mob')
# print(product_price.text)

price =[]
for wine in product_price:
    wineprice = wine.span.text
    price.append(wineprice)
    print(f'''Wine Price: {wineprice}''')
# print(f'''\nWine Price: {price}\n''')


image =[]
product_image_link = (soup.find_all('div', class_='thumbnail-image'))
# print(product_image_link)
for imagelink in product_image_link:
    wineimagelink = imagelink.a['href']
    image.append(wineimagelink)
    # image.append(imagelink)
    print(f'''Wine Image Lin: {wineimagelink}''')
# print(f'''\nWine Image: {image}\n''')
#
#
# """ writing data to CSV """
# open OrganicWine2.csv file in "write" mode
# newline stops a blank line appearing in csv

with open('OrganicWine2.csv', 'w',newline='') as file:
  # create a "writer" object
  writer = csv.writer(file, delimiter=',')
  # use "writer" obj to write
  # you should give a "list"
  writer.writerow(["Wine Name", "Wine Price", "Wine Image Link"])
  for i in range(23):
    writer.writerow([
      winename[i],
      price[i],
      image[i],
    ])