Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法在excel文件中写入新填充的结果(源自反向搜索)_Python_Excel_Python 3.x_Web Scraping_Openpyxl - Fatal编程技术网

Python 无法在excel文件中写入新填充的结果(源自反向搜索)

Python 无法在excel文件中写入新填充的结果(源自反向搜索),python,excel,python-3.x,web-scraping,openpyxl,Python,Excel,Python 3.x,Web Scraping,Openpyxl,我已经用python编写了一个脚本,它能够从excel文件中获取搜索输入,将其放入我的scraper中的输入值变量中,并获取与搜索参数匹配的图像链接。当我打印它时,我会得到准确的结果。我正在使用openpyxl读取和写入值 然而,当我试图在一个新的excel文件中的每个搜索关键字旁边写入填充的结果时,我被卡住了 在我当前的excel文件中,有三个名为item.xlsx的搜索关键字(三部电影)。它们是: Shutter Island Black Swan True Grit 如果我运行现有刮板

我已经用python编写了一个脚本,它能够从excel文件中获取
搜索输入
,将其放入我的scraper中的
输入值
变量中,并获取与搜索参数匹配的
图像链接
。当我打印它时,我会得到准确的结果。我正在使用
openpyxl
读取和写入值

然而,当我试图在一个新的excel文件中的每个搜索关键字旁边写入填充的结果时,我被卡住了

在我当前的excel文件中,有三个名为
item.xlsx
的搜索关键字(三部电影)。它们是:

Shutter Island 
Black Swan
True Grit
如果我运行现有刮板而不进行任何修改,则新文件中的结果如下所示:

Shutter Island    
Black Swan        
True Grit         
image_link 1
image_link 2
image_link 3
我希望我的刮板将获取图像链接,并将它们写在一个新的excel文件中的每部电影旁边。因此,输出应如下所示:

Column A          Column B
Shutter Island    image_link 1
Black Swan        image_link 2
True Grit         image_link 3
这就是我迄今为止所尝试的:

import requests
from bs4 import BeautifulSoup
from openpyxl import load_workbook

wb = load_workbook('item.xlsx')
ws = wb['Sheet1']

for row in range(1, ws.max_row + 1):
    input_val = ws["A" + str(row)].value  #the search keyword holds here

    response = requests.get("http://www.boxofficemojo.com/search/?",params = {'q':input_val})
    soup = BeautifulSoup(response.text,"lxml")
    table = soup.select("table")[1]
    for items in table.select('tr')[4:5]:
        [elem.extract() for elem in soup.select("script")] #kicking out script from result
        data = [img['src'] for img in items.select('td img')]
        ws.append(data)
        wb.save("new_one.xlsx")

下面我提供了一个例子,说明如何做到这一点。在B列中,有一个url字符串。我还添加了第三列,它利用excel中的
HYPERLINK
功能显示a列的文本,并链接到B列中提供的所需url

import requests
from bs4 import BeautifulSoup
from openpyxl import load_workbook

wb = load_workbook('item.xlsx')
ws = wb['Sheet1']

for row in range(1, ws.max_row + 1):
    input_val = ws["A" + str(row)].value  #the search keyword holds here
    response = requests.get("http://www.boxofficemojo.com/search/?",params = {'q':input_val})
    soup = BeautifulSoup(response.text,"lxml")
    table = soup.select("table")[1]
    for items in table.select('tr')[4:5]:
        [elem.extract() for elem in soup.select("script")] #kicking out script from result
        data = [img['src'] for img in items.select('td img')]
        if row != 1:
            cell_str = data[0]
            ws.cell(row=row, column=2).value = '%s' % (cell_str)
            ws.cell(row=row, column=3).value = '=HYPERLINK(B%d, A%d)' % (row, row)
        else:
            ws.cell(row=row, column=2).value = 'Column B'
            ws.cell(row=row, column=3).value = 'Hyperlink'
        wb.save("new_one.xlsx")
输出:

我从来没想到会有这么理想的答案。你就是传奇人物@patrickjlong1。只要对
else
块做一点简单的解释,如果你知道任何链接,我可以在这里学习你的方法。再次感谢。也许,我已经理解您使用
else
块来编写标题。现在,我唯一的期望是你可以为我提供任何链接跟踪,我可以学习这样的理想用法。谢谢@初学者编码器。现在您提到它,
else
语句有点不必要,您可以跳过
else
和hardcode
row=1
。我在openpyxl中发现了这个选项,它为您提供了一些其他超链接选项。