eziceice / thinkinginpython

Study notes for Python 3
4 stars 0 forks source link

Python & Web #17

Open eziceice opened 6 years ago

eziceice commented 6 years ago

屏幕抓取

eziceice commented 6 years ago

Tidy和XHTML解析

class Scraper(HTMLParser): in_h3 = False in_link = False

def handle_starttag(self, tag, attrs):
    attrs = dict(attrs)
    if tag == 'h3':
        self.in_h3 = True

    if tag == 'a' and 'herf' in attrs:
        self.in_link = True
        self.chunks = []
        self.url = attrs['href']

def handle_data(self, data):
    if self.in_link:
        self.chunks.append(data)

def handle_endtag(self, tag):
    if tag == 'h3':
        self.in_h3 = False
    if tag == 'a':
        if self.in_h3 and self.in_link:
            print(self.chunks, self.url)
            self.in_link = False

text = request.urlopen('https://www.google.com').read() text = bytes.decode(text) parser = Scraper() parser.feed(text) #解析HTML parser.close()

eziceice commented 6 years ago

Beautiful Soup

Once upon a time there were three little sisters; and their names were Elsie, Lacie and Tillie; and they lived at the bottom of a well.

...

"""

from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') print(soup.title) print(soup.head) print(soup.title.string) print(soup.find_all('a'))

eziceice commented 6 years ago

使用CGI技术创建动态网页

Windows: 一定要使用Python的全路径

!C:\Python22\python.exe


- Step 3: 设置文件权限, 最后一件事是设置合适的文件权限.要确保每个人都可以读取和执行脚本文件, 但是还要确保只有你可以写入. 修改文件权限的UNIX命令是chmod. 只要运行以下的命令即可(如果脚本叫做somescript.cgi), 使用普通的用户账户, 或者为这类Web任务特别建立的账户: 在做好所有这些准备后, 应该能将脚本作为网页打开并且执行. 

chmod 755 somescript.cgi


- 一般来说不允许CGI脚本修改计算机上的任何文件. 如果想要它修改文件, 必须显式地给它设置相应的权限. 这时有两个选项, 如果有root(系统管理员)权限的话, 那么可以为你的脚本创建一个用户账户, 改变需要修改的文件的所有权. 如果没有root权限, 则可以为文件设置文件权限, 这样系统上的所有用户(包括网络服务器用来运行CGI脚本的用户)都被允许写文件. 

chmod 666 editable_file.txt #使用666可能会导致潜在危险

eziceice commented 6 years ago

Web服务