简体   繁体   中英

Python: Problems finding string in website source code

I open a website with urlopen. I then put the website sourcecode into a variable like so

source = website.read()

When I just print the source it comes out formatted correctly, however when I try to iterate through each line each character is it's own line.

for example

when I just print it looks like this

<HTML> title</html>

When I do this

for line in source:
      print line

it looks like this

<
H
T
M
L
... etc

I need to find a string that starts with "var" and then print that entire line.

使用readlines()而不是read()获得行列表。

Or use:

for line in source.split("\n"):
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM