Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 提取链接文本并写入文件_Python_Web Crawler - Fatal编程技术网

Python 提取链接文本并写入文件

Python 提取链接文本并写入文件,python,web-crawler,Python,Web Crawler,我有一个爬虫程序,它只在链接文本包含给定文本并且我正在将输出写入html文件时才从页面中提取链接。它的工作,但我想添加整个链接文本旁边的这些链接像这样-“初级Java开发人员”-我如何才能做到这一点 谢谢 import requests from bs4 import BeautifulSoup import re def jobs_crawler(max_pages): page = 1 file_name = 'links.html' while page <

我有一个爬虫程序,它只在链接文本包含给定文本并且我正在将输出写入html文件时才从页面中提取链接。它的工作,但我想添加整个链接文本旁边的这些链接像这样-“初级Java开发人员”-我如何才能做到这一点

谢谢

import requests
from bs4 import BeautifulSoup
import re

def jobs_crawler(max_pages):
    page = 1
    file_name = 'links.html'

    while page < max_pages:
        url = 'https://www.jobs.cz/prace/praha/?field%5B%5D=200900011&field%5B%5D=200900012&field%5B%5D=200900013&page=' + str(page)
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        page += 1
        file = open(file_name,'w')

        for link in soup.find_all('a', {'class': 'search-list__main-info__title__link'}, text=re.compile('IT', re.IGNORECASE)):
            href = link.get('href') + '\n'
            file.write('<a href="' + href + '">'+ 'LINK TEXT HERE' + '</a>' + '<br />')
            print(href)
        file.close()

    print('Saved to %s' % file_name)

jobs_crawler(5)
导入请求
从bs4导入BeautifulSoup
进口稀土
def jobs_爬虫程序(最大页面数):
页码=1
文件名='links.html'
当页面<最大页面数时:
url='1〕https://www.jobs.cz/prace/praha/?field%5B%5D=200900011&field%5B%5D=200900012&field%5B%5D=200900013&page=“+str(第页)
source_code=requests.get(url)
纯文本=源代码.text
soup=BeautifulSoup(纯文本)
页码+=1
file=open(文件名为'w')
对于soup.find_all('a',{'class':'search-list_uuumain-info_uuutitle_uulink'},text=re.compile('IT',re.IGNORECASE)):
href=link.get('href')+'\n
file.write(“”+“
”) 打印(href) file.close()文件 打印('保存到%s'%1!''文件名) 作业爬虫(5)
试试这个:--

这应该会有所帮助

file.write('''<a href="{0}">{1}</a><br />'''.format(link.get('href'), link.text ))
file.write(“”“
”“。格式(link.get('href'),link.text))
在这种情况下可能会出现重复-可能只是
链接。text
就可以了…谢谢,这太棒了!!
file.write('''<a href="{0}">{1}</a><br />'''.format(link.get('href'), link.text ))